This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
76 lines (62 loc) · 1.45 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* eslint no-console: 0
*/
'use strict';
let express = require('express'),
compression = require('compression'),
cors = require('cors'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
app = express(),
isProduction = process.env.NODE_ENV === 'production';
// Load the env variables only in DEV mode
if (!isProduction) {
require('dotenv').load();
}
// Set JWT secret on the app object
app.set('superSecret', process.env.SECRET);
// use morgan to log requests to the console
if (!isProduction) {
app.use(morgan('dev'));
}
// compress all requests
app.use(compression());
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(bodyParser.json());
// Enable CORS
app.use(
cors({
allowedHeaders: [
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, x-access-token'
]
})
);
let port = process.env.PORT || 8000; // set our port
app.use(require('./server/routes'));
app.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
res.status(err.status || 500).json({
error: err.message
});
});
// catch 404 errors
app.use((req, res) => {
let err = new Error('Not Found');
res.status(404).json({
error: err.message
});
});
// START THE SERVER
app.listen(port);
console.log('Listening on port', port);
// Export the app object
module.exports = app;