Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var express = require('express'),
Mongo = require('mongodb'),
GridStore = Mongo.GridStore,
Server = Mongo.Server,
ObjectID = Mongo.ObjectID,
Db = Mongo.Db;
var app = express(),
server = new Server('localhost', 27017, {auto_reconnect: true}),
db = new Db('example', server);
db.open(function(err, db) {
if (err) {
console.log('Could not connect to MongoDB');
} else {
console.log('Connected to MongoDB');
}
})
app.set('port', process.env.PORT || 8080);
app.use(app.router);
app.get('*', function (req, res) {
var id = '.' + req.path;
// Check for exist
GridStore.exist(db, id, function (err, result) {
if (true !== result) {
return res.send(404);
}
// Create GridStore in READ mode
new GridStore(db, id, "r").open(function (err, gridStore) {
if (err) throw err;
// Get read stream
var readStream = gridStore.stream(true);
readStream.on('close', function () {
return;
});
// Simply 'pipe' read stream to write stream
readStream.pipe(res);
});
});
});
app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.