-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
49 lines (40 loc) · 1.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
import express from 'express';
import logger from 'morgan';
import bodyParser from 'body-parser';
import http from 'http';
import path from 'path';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import winston from 'winston';
import webpackHotMiddleware from 'webpack-hot-middleware';
import webpackConfig from './webpack.config.dev';
import routes from './server/routes';
const app = express();
const router = express.Router();
const port = parseInt(process.env.PORT, 10) || 8000;
const compiler = webpack(webpackConfig);
app.set('port', port);
// Webpack config
app.use(webpackMiddleware(compiler, {
hot: true,
publicPath: webpackConfig.output.publicPath,
noInfo: true,
}));
app.use(webpackHotMiddleware(compiler));
// Log requests to the console.
app.use(logger('dev'));
// Parse incoming requests data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Use router for our routes
routes(router);
app.use('/api', router);
// Setup a default catch-all route that sends back a welcome message in JSON format
app.get('/*', (req, res) => {
res.status(200).sendFile(path.join(__dirname, './server/index.html'));
});
const server = http.createServer(app);
server.listen(port, () => {
winston.info(`The server is running at localhost:${port}`);
});
export default app;