Created
January 19, 2016 05:45
-
-
Save alexhidalgo/3b2eeca8bfa1aeb13a49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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