-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
102 lines (67 loc) · 2.32 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
const ROOT_DIR = process.env.ROOT_DIR = __dirname;
const SERVER_PORT = process.env.PORT || 8989;
// Pull in environmental variables
require('dotenv').config({ path: `${ROOT_DIR}/.env` });
// Import connections
require(`${ROOT_DIR}/config/mongoose`);
require(`${ROOT_DIR}/config/redis`);
// Import Components
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var logger = require(`${ROOT_DIR}/config/winston`);
var nunjucksENV = require(`${ROOT_DIR}/config/nunjucksENV`);
// Import Routes
var viewRoutes = require(`${ROOT_DIR}/lib/routes/views/views.route`);
var apiRoutes = require(`${ROOT_DIR}/lib/routes/api/api.route`);
// CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
res.removeHeader('x-powered-by');
next();
});
app.use(express.static(`${ROOT_DIR}/public/static`));
app.set('view engine', 'nunjucks');
nunjucksENV.express(app);
app.options('*', (req, res, next) => { res.send(''); });
app.use(bodyParser.json({ limit: '1mb' }));
app.use(cookieParser());
// Configure routes
app.use('/', viewRoutes);
app.use('/api', apiRoutes);
// Configure Error Handling
app.all('*', (req, res, next) => {
res.status(404);
// DEPENDING ON API OR VIEW, RENDER 404 OR SEND 404
// res.render('404');
res.send({
'status': res.statusCode,
'message': 'No resource exists at this route.'
});
});
app.use((err, req, res, next) => {
if (err.status == 404) { return next(err); }
var errorStatus = err.status || 500;
if (res.statusCode == 200) { res.status(errorStatus); }
var status = res.statusCode;
var message = err.message || 'no message';
var stack = err.stack || 'no stack';
var raw = err._raw || 'no raw';
logger.log('error', { 'status': status, 'message': message, 'stack': stack, 'raw': raw });
next(err);
});
app.use((err, req, res, next) => {
res.send({
'status': res.statusCode,
'message': err.message
});
if (err.stack) { console.log(err.stack); }
});
// Start the server
app.listen(SERVER_PORT, () => {
console.log(`:: express server listening @ port ${SERVER_PORT} ::`);
});