-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·81 lines (67 loc) · 2.6 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// server.js
// what we call to turn the app on.
'use strict';
// modules ==========================================================
const express = require('express'),
app = express(),
http = require('http'),
path = require('path'),
_ = require('lodash'),
server = http.createServer(app),
mongoose = require('mongoose'),
// EXPRESS MODULES ==================================================
morgan = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
// SESSION STORAGE ==================================================
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
sessionStore = new MongoStore({
mongooseConnection: mongoose.connection
}),
// PROCESS PORTS =====================================================
port = Number(process.env.PORT || 8081),
// Database summoning ===============================================
database = require('./config/db'),
db = database.db,
router = require('./server/routes/router');
// Detailed logging ================
// app.use(morgan('combined'));
// GLOBAL VARIABLES =================================================
app.locals = (!!process.env.APP_SECRET_KEY) ? {
cookie_name: 'connect.sid',
secret: process.env.APP_SECRET_KEY
} :
_.merge(app.locals, require(path.join(__dirname, 'secrets')));
// CONFIGURATION ====================================================
global.rootRequire = (name) => require(__dirname + '/' + name);
// express 4.0 basic configuration ==================================
app.use(cookieParser());
app.use(express.static(__dirname + '/dist'));
app.use(bodyParser());
// session start ====================================================
app.use(session({
secret: process.env.APP_SECRET_KEY || app.locals.secret,
cookie: {
path: '/',
expires: false, // Alive Until Browser Exits
// secure: true, // TODO: implement https
httpOnly: true
},
store: sessionStore,
saveUninitialized: true, // (default: true)
resave: true // (default: true)
}));
// server /api/ routes -> replies to /etc requests ==================
app.use('/', router);
// DEFAULT ROUTE ====================================================
app.use('*', function(req, res) {
res.status(404) // HTTP status 404: NotFound
.send('404 - Route not found');
});
// Turn it on
server.listen(port, function() {
console.log('listening on', port);
});
// EXPOSE APP AS OBJECT =============================================
exports = module.exports = app;