Skip to content

Instantly share code, notes, and snippets.

@alexhidalgo
Created January 19, 2016 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhidalgo/3b2eeca8bfa1aeb13a49 to your computer and use it in GitHub Desktop.
Save alexhidalgo/3b2eeca8bfa1aeb13a49 to your computer and use it in GitHub Desktop.
var express = require('express'),
app = express(),
engines = require('consolidate'),
MongoClient = require('mongodb').MongoClient,
assert = require('assert')
app.engine('html', engines.nunjucks)
app.set('view engine', 'html')
app.set('views', __dirname + '/views')
MongoClient.connect('mongodb://localhost:27017/test', function (err, db) {
assert.equal(null, err)
console.log('Successfully connected to MongoDB')
app.get('/', function (req, res) {
// Express will create an appropriate HTTP response including, length, headers, etc.
// and content with 'Hello.. .' So express does a lot of work for us here
db.collection('names').find({}).toArray(function (err, docs) {
console.log(docs)
res.render('Hello', { 'names': docs})
})
})
app.use(function (req, res) {
res.sendStatus(404)
})
var server = app.listen(3000, function () {
var port = server.address().port
console.log('Express server listening on port %s', port)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment