Using Nodejs & Expressjs to Upload and save Image Files into MongoDB (Part 2/3)

1101-sdt-mongoThis is a follow-on to Part 1, an introductory post

This example will use the Expressjs templating engine. Expressjs can be a deep and mind boggling domain but we’ll use only two of its key features, routes and views.

This example will NOT use mongdb GridFS. In the interests of keeping this as simple as possible, we’ll limit our image file size to < 2MB and avoid helper libraries that obfuscate what’s going on.

So assuming you have nodejs working you can stub out a templated app. I’m going to use Express Application Generator to get a directory structure and some file templates going. I’ll also install nodemon, a useful utility which restarts the nodejs service each time you save a file change.

sudo npm install express-generator nodemon -g

I’m choosing to use the ejs templating engine instead of the default Jade engine. If you’re unfamiliar with Jade, it looks weird. A bit like going straight to Coffeescript and bypassing Javascript.

express --ejs --git --testdir

Install two NPM packages

install mongodb multer --save

You may want to run this to make sure packages and dependencies are up to date.

npm install

Now start your node service from within your testdir and you should be able to access it from your browser with http://localhost:3000 (or whatever port it starts on)

nodemon bin/www

Only two files are really needed for this example,

  1. routes/index.js
  2. views/index.ejs

Open each in your code editor and we’ll start with views/index.ejs. This is the minimum HTML necessary to get a ‘choose file’ button and file selector going. It also includes a text field for some user entered meta data.

<form action="/uploadpicture" method="POST" enctype="multipart/form-data">
<input type="file" name="picture" accept="application/x-zip-compressed,image/*">

<input class="form-control" type="text" name="description" placeholder="Description or Message">

<input class="btn btn-primary" type="submit" value="submit">

Save this file and goto the routes/index.js

We’re going to create two routes. You’ll want it to look something like this,

var express = require('express')
   , router = express.Router()
   , MongoClient = require('mongodb').MongoClient
   , ObjectId = require('mongodb').ObjectId
   , fs = require('fs-extra')
   // Your mLabs connection string
   , url = 'mongodb://'
   , multer = require('multer')
   , util = require('util')
   , upload = multer({limits: {fileSize: 2000000 },dest:'/uploads/'})

// Default route http://localhost:3000/
router.get('/', function(req, res){ res.render('index'); });

// Form POST action handler'/uploadpicture', upload.single('picture'), function (req, res){

if (req.file == null) {
  // If Submit was accidentally clicked with no file selected...
  res.render('index', { title:'Please select a picture file to submit!'); });
} else {

MongoClient.connect(url, function(err, db){
   // read the img file from tmp in-memory location
   var newImg = fs.readFileSync(req.file.path);
   // encode the file as a base64 string.
   var encImg = newImg.toString('base64');
   // define your new document
   var newItem = {
      description: req.body.description,
      contentType: req.file.mimetype,
      size: req.file.size,
      img: Buffer(encImg, 'base64')

   .insert(newItem, function(err, result){
   if (err) { console.log(err); };
      var newoid = new ObjectId(result.ops[0]._id);
      fs.remove(req.file.path, function(err) {
         if (err) { console.log(err) };
         res.render('index', {title:'Thanks for the Picture!'});

This should be about all you need to get a basic uploader running…

One important note here is that we’re converting the binary image file (jpg) into a text string that is base64 encoded. This is not the nodejs default (utf-8). The reason for this is not well documented according to Google but will become apparent in the next post.

The next post will examine how to extract these image files from mongodb and serve them up over http.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s