-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (40 loc) · 1.37 KB
/
index.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
global._ = require('lodash');
global.t = require('moment');
var cookieParser = require('cookie-parser'),
Firebase = require("firebase"),
env = process.env.NODE_ENV || 'development',
config = require('./config/config')[env],
routes = require('./api/routes'),
express = require('express'),
bodyParser = require('body-parser'),
app = express();
(function run(appdir, rootRefUrl) {
app.use(cookieParser());
app.dir = appdir;
// things to do on each request
app.use(function (req, res, next) {
// log each request in development environment
if(env !== 'production') console.log(t().format('HH:MM'), req.method, req.url, req.socket.bytesRead);
// tell the client what firebase to use
res.cookie('rootRef', rootRefUrl);
next();
});
// static files
app.use(express.static(app.dir + '/public'));
// Standard error handling
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500).send('Something broke!');
});
// to support JSON-encoded bodies
app.use(bodyParser.json());
// to support URL-encoded bodies
app.use(bodyParser.urlencoded({
extended: true
}));
routes(app, config);
var server = app.listen(process.env.PORT || 1717, function() {
console.log('Listening on port %d', server.address().port);
});
})(process.cwd(), config.firebase.rootRefUrl);
module.exports = app;