forked from amnotafraid/mm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (52 loc) · 1.51 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
/**
* This is the main Express app.
*/
// Dependencies
const express = require('express');
const favicon = require('serve-favicon');
const fs = require('fs');
const http = require('http');
const path = require('path');
// Express Middleware
const bodyParser = require('body-parser');
const morgan = require('morgan');
// Read `.env` into `process.env`
require('dotenv').config({
silent: true,
});
// Load nconf environment variable defaults
require('nconf').env().defaults({
NODE_ENV: 'development',
});
// Load environment
const env = require('./env');
// Create Express App
const main = express();
// HTTPS Forwarding
// Needed for Heroku / Load Balancers
main.enable('trust proxy');
// Body parsing
main.use(bodyParser.json());
main.use(bodyParser.urlencoded({ extended: true }));
// Get session ID from header
main.use((req, res, next) => {
req.sessionID = req.get('x-session') || null;
next();
});
//Serve Angular App to Browser upon GET / request
main.use(express.static(path.join(__dirname, 'dist')));
var faviconPath = path.join(__dirname, 'dist/assets', 'razoyo-favicon.gif');
if (fs.existsSync(faviconPath)) {
main.use(favicon(faviconPath));
}
// Logging
main.use(morgan('short'));
//
// Use API routes
main.use(require('./api')(env));
// Start the HTTP server
const server = http.createServer(main);
server.listen(env.PORT, () => {
// eslint-disable-next-line no-console
console.log(`\nExpress Server [PORT: ${env.PORT}] [NODE_ENV: ${env.NODE_ENV}]\n`);
});