-
Notifications
You must be signed in to change notification settings - Fork 16
/
server.js
38 lines (31 loc) · 1000 Bytes
/
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
/* global __dirname */
import express from 'express';
import next from 'next';
import { argv } from 'yargs';
import openBrowser from 'react-dev-utils/openBrowser';
const port = parseInt(argv.port, 10) || parseInt(process.env.PORT, 10) || 3333;
const dev = process.env.NODE_ENV !== 'production';
const createServer = (name = 'Server', modifyServer = () => {}) => {
const server = express();
server.get('/api/alive', async (req, res) => {
res.json(true);
});
modifyServer(server);
server.listen(port, err => {
if (err) throw err;
console.info(`> ${name} ready on http://localhost:${port}`);
if (argv.open) {
openBrowser(`http://localhost:${port}/demo`);
}
});
};
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer('Server', server => {
server.use('/node_modules', express.static(__dirname + '/node_modules'));
server.get('*', (req, res) => {
return handle(req, res);
});
});
});