title: Using MongoDB, Express, Node.Js and GridFS-stream for storing video and picture files
body: <p>I am creating a single page application using JavaScript(JQuery) and need to store large video files which size exceed 16Mb. I found that need to use GridFS supporting large files. As I am the new one to MongoDB I am not sure how to use GridFS. There are some good tutorials on creating applications using Node.js, MongoDB and Express but cant find any describing how to use GridFS with MongoDB (not mongoose), Express and Node.js. I managed to put up stuff for uploading files in the BSON-document size limit of 16MB. This is what I have: </p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var bodyParser = require('body-parser');
  var mongo = require('mongodb');
  var monk = require('monk');
  var Grid = require('gridfs-stream');
  
  var db = monk('localhost:27017/elearning');
  var gfs = Grid(db, mongo);
  
  var routes = require('./routes/index');
  var users = require('./routes/users');
  var courses = require('./routes/courses');
  
  var app = express();
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  
  
  app.use(logger('dev'));
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  // Make our db accessible to our router
  app.use(function(req,res,next){
    req.db = db;
    next();
  });
  
  app.use('/', routes);
  app.use('/users', users);
  app.use('/courses', courses);
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handlers
  
  // development error handler
  // will print stacktrace
  if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
      res.status(err.status || 500);
      res.render('error', {
        message: err.message,
        error: err
      });
    });
  }
  
  // production error handler
  // no stacktraces leaked to user
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: {}
    });
  });
  
  
  module.exports = app;
  </code></pre>
  
  <p>And, for example, the courses file is as the following:</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  
  /* GET courses listing */
  router.get('/courselist', function(req, res) {
      var db = req.db;
      var collection = db.get('courselist');
      collection.find({},{},function(e,docs){
          res.json(docs);
      })
  
  });
  
  /* POST courses data */
  router.post('/courselist', function(req, res) {
      var db = req.db;
      var collection = db.get('courselist');
      collection.insert(req.body, function(err, result){
          res.send(
              (err === null) ? { msg: '' } : { msg: err }
          );
      });
  });
  
  /* Delete courses data */
  router.delete('/courselist/:id', function(req, res) {
      var db = req.db;
      var collection = db.get('courselist');
      var userToDelete = req.params.id;
      collection.remove({ '_id' : userToDelete }, function(err) {
          res.send((err === null) ? { msg: '' } : { msg:'error: ' + err });
      });
  });
  
  module.exports = router;
  </code></pre>
  
  <p>I would be extremely grateful for your help, if you could tell how should I edit above files in order to utilize GridFS and be able to get, upload and delete video and picture files from my elearning database.  </p>