-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (51 loc) · 1.61 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
59
60
61
62
63
var fs = require('fs');
var http = require('http');
var join = require("path").join;
var express = require('express');
var lessMiddleware = require('less-middleware');
var session = require('express-session');
var db = require('./lib/db');
var app = express();
var cookieParser = require('cookie-parser')('secret');
var MongoStore = require('connect-mongo')({session: session});
var sessionStore = new MongoStore({
db: db.db
});
var publicDir;
var env = process.env.NODE_ENV || 'development';
app.use(require('body-parser')());
app.use(require('method-override')());
app.use(cookieParser);
app.use(session({ store: sessionStore }));
if ('development' == env) {
publicDir = join(__dirname, "public");
app.use(lessMiddleware({
src: publicDir,
dest: publicDir,
sourceMap: true
}));
app.use(express.static(publicDir));
// for less source maps
app.use('/' + publicDir.replace(/\\/g, '/'), express.static(publicDir));
}
if ('production' == env) {
publicDir = join(__dirname, "build");
app.use(express.static(publicDir, { maxAge: 86400000 }));
}
var server = http.createServer(app);
var io = require('./lib/socket')(server, cookieParser, sessionStore);
require('./lib/auth')(app);
app.get('*', function (req, res) {
res.sendfile(join(publicDir, 'index.html'));
});
db.promise.then(function () {
var port = process.env.PORT || 8000;
var oldUmask = process.umask(0);
if (fs.existsSync(port)) {
fs.unlinkSync(port);
}
server.listen(port, function () {
console.log('Server listening on port ' + port);
process.umask(oldUmask);
});
});