Skip to content

Instantly share code, notes, and snippets.

@kbroman
Last active August 29, 2015 14:21
Show Gist options
  • Save kbroman/feedc0cc2489a089aa68 to your computer and use it in GitHub Desktop.
Save kbroman/feedc0cc2489a089aa68 to your computer and use it in GitHub Desktop.
// hello world with node + express + consolidate + swig + mongodb
//
// grab name from MongoDB doc (course db, hello_mongo_express collection)
//
// type 'node hello_mongo.js' and go to http://localhost:8080
var express = require('express'),
app = express(),
cons = require('consolidate'),
MongoClient = require('mongodb').MongoClient;
app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + "/views");
app.get('/', function(req, res){
MongoClient.connect('mongodb://localhost/course', function(err, db) {
if(err) throw err;
db.collection("hello_mongo_express").findOne({}, function(err, doc) {
res.render('hello', doc);
});
});
});
app.get('*', function(req, res){
res.status(404).send('Page Not Found');
});
app.listen(8080);
console.log('Express server started on port 8080');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment