-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
51 lines (42 loc) · 1.52 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
'use strict';
const express = require('express');
const app = module.exports = express();
const port = process.env.PORT || 8080;
// Set up configuration
app.config = require('./lib/config/config');
// Parse JSON body on requests
app.use(express.json());
// trust webpack or Apache proxy
app.set('trust proxy', 1);
if (process.env.NODE_ENV === 'production'){
console.info('Starting app in production mode.');
} else {
console.info('Starting app in development mode.', process.env.NODE_ENV);
// this will allow you to set up a remote server that other devs can access locally
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', "x-token, Content-Type");
res.header('Access-Control-Allow-Credentials', true);
// Needed when using graphQL
if ('OPTIONS' === req.method) {
res.sendStatus(200);
}
else {
next();
}
});
}
// all of the controller files
app.use('/words', require('./lib/controllers/words'));
app.use('/classes', require('./lib/controllers/classes'));
const server = app.listen(port, () => {
const host = server.address().address;
const port = server.address().port;
console.log('Server listening at http://%s:%s', host, port);
});
server.on('error', (error) => {
console.error(`Post ${port} in use, exiting!`);
process.exit(1);
});
module.exports = app;