-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (56 loc) · 2.12 KB
/
server.js
File metadata and controls
77 lines (56 loc) · 2.12 KB
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
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const path = require('path');
const webpack = require('webpack');
const webpackConfigDev = require('./webpack.config');
const webpackConfigProd = require('./webpack.config.prod.js');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackMiddleware = require('webpack-dev-middleware');
// let webpackConfig;
// if (process.env.NODE_ENV === 'production') {
// webpackConfig = webpackConfigProd;
// } else {
// webpackConfig = webpackConfigDev;
// }
const webpackConfig =
process.env.NODE_ENV === 'production' ? webpackConfigProd : webpackConfigDev;
const compiler = webpack(webpackConfig);
// This kills all running nodemon processes before restarting.
// To resolve the listen EADDRINUSE error
process.on('SIGUSR2', () => { process.exit(0); });
const serverPath =
process.env.NODE_ENV === 'production' ? 'server-dist' : 'server';
const authentication = require(`./${serverPath}/middleware/authentication`); // eslint-disable-line
const dotenv = require('dotenv');
dotenv.config();
// Set up the express app
const app = express();
const port = parseInt(process.env.PORT, 10) || 5000;
app.set('port', port);
// Log requests to the console.
app.use(logger('dev'));
app.use(webpackMiddleware(compiler));
app.use(
webpackHotMiddleware(compiler, {
hot: true,
publicPath: webpackConfig.output.path,
noInfo: true
})
);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// require jwt token for authenticated routes
app.use('/api', authentication.verifyToken);
app.use('/auth/api', authentication.verifyToken);
require(`./${serverPath}/routes`)(app); // eslint-disable-line
app.use(express.static(path.resolve(`${__dirname}/public`)));
// Setup a default catch-all route that sends back a
// welcome message in JSON format.
app.get('*', (request, response) => {
response.sendFile(path.resolve(`${__dirname}/public/index.html`));
});
app.listen(port, () => {
console.log(`\nApplication is running in ${app.get('env')} on port ${port} `);
});
module.exports = app;