Skip to content

Instantly share code, notes, and snippets.

@MadaraUchiha
Created September 11, 2014 21:18
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 MadaraUchiha/5e587b66fb0ea6a49cbf to your computer and use it in GitHub Desktop.
Save MadaraUchiha/5e587b66fb0ea6a49cbf to your computer and use it in GitHub Desktop.
My current version of the main JS file. Still in development.
/**
* 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