-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
47 lines (33 loc) · 1.23 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
import 'dotenv/config';
import 'isomorphic-fetch';
import express from 'express';
import path from 'path';
import compression from 'compression';
import helmet from 'helmet';
import bodyParser from 'body-parser';
import {api} from './routes';
// the reactified route-handler from the `app`
import reactHandler from '../app/_server.js';
// create express app...
export const app = express();
const {APP_WEB_BASE_PATH} = process.env;
// middleware
app.use(compression());
app.use(helmet());
app.use(`${APP_WEB_BASE_PATH}/static`, express.static(path.join(__dirname, 'static')));
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use(`${APP_WEB_BASE_PATH}/api`, api);
// handle routes via react...
app.get("*", reactHandler);
// prepare 404
app.use("*", (req, res, next) => { // eslint-disable-line
next({status: 404, message: "Not Found"});
});
// handle any errors
app.use( (err, req, res, next) => { // eslint-disable-line
res.status(err.status||500).send(err.message || "Application Error");
console.error(err.status===404?`404 ${req.url}`: err.stack); // eslint-disable-line
});
const { PORT } = process.env;
app.listen(PORT, () => console.log('Running on port ' + PORT)); // eslint-disable-line