Created
September 11, 2014 21:18
-
-
Save MadaraUchiha/5e587b66fb0ea6a49cbf to your computer and use it in GitHub Desktop.
My current version of the main JS file. Still in development.
This file contains 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
/** | |
* Created by dor.t on 8/24/2014. | |
*/ | |
var express = require("express"), | |
path = require('path'), | |
Promise = require('bluebird'), | |
mongo = require('mongodb').MongoClient, | |
mongoConn, | |
app = express(); | |
Promise.promisifyAll(mongo); | |
Promise.promisifyAll(require('mongodb/lib/mongodb/db').Db.prototype); | |
Promise.promisifyAll(require('mongodb/lib/mongodb/collection').Collection.prototype); | |
Promise.promisifyAll(require('mongodb/lib/mongodb/cursor').Cursor.prototype); | |
mongoConn = mongo.connectAsync("mongodb://127.0.0.1:27017/test"); | |
app.set('views', path.join(__dirname, 'views')); | |
app.engine('html', require('hogan-express')); | |
app.set('view engine', 'html'); | |
app.use(function getUserData(req, res, next) { | |
mongoConn.then(function (db) { | |
var collection = db.collection('users'); | |
return collection.find({"userName": req.headers.host.split('.')[0]}).limit(1).toArrayAsync() | |
}) | |
.then(function (results) { | |
results = results[0]; | |
//Todo: Check if results is empty, and render 404 page if it is. | |
res.userInfo = results; | |
console.log(results); | |
}).catch(function (err) { | |
res.userInfo = "No information available! " + err; //Todo: Render 500 page. | |
}).then(function () { | |
next(); | |
}); | |
}); | |
app.use('/', require('./routes/index')); | |
var server = app.listen(3000, function () { | |
console.log("Listening on port %d", server.address().port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment