This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
58 lines (48 loc) · 1.44 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
var express = require('express');
var mongoose = require('mongoose');
var config = require('./config');
var helpers = require('./lib/helpers');
var route = require('./lib/route');
var User = require('./lib/models/user');
var app = express();
var running = false;
function sessionUser(req, res, next) {
res.locals.user = req.session.user &&
new User({username: req.session.user});
next();
}
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.locals(helpers);
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('secret'));
app.use(express.session());
app.use(express.csrf());
app.use(express.static(__dirname + '/public'));
app.use(sessionUser);
app.use(app.router);
app.use(express.errorHandler({showStack: true, dumpExceptions: true}));
});
route.init(app);
exports.run = function (callback) {
if (running) {
return callback()
}
mongoose.connect(config.db);
mongoose.connection.on(
'error', console.error.bind(console, 'connection error:'));
mongoose.connection.once('open', function () {
console.log('Server is listening on port ' + config.port);
app.listen(config.port);
running = true;
callback();
});
};
if (require.main === module) {
exports.run(function (err) {
err && console.error(err);
});
}