1 | const express = require('express');
|
2 | const morgan = require('morgan');
|
3 | const bodyParser = require('body-parser');
|
4 |
|
5 | const {
|
6 | appRootPath,
|
7 | getServerConfiguration,
|
8 | cors,
|
9 | routes,
|
10 | proxy
|
11 | } = require('./utils');
|
12 |
|
13 | const Server = () => {
|
14 | const { createServer } = require('http');
|
15 |
|
16 | const config = getServerConfiguration(appRootPath('.devtools.json'));
|
17 | const appRootPathDist = appRootPath(config.distRoot);
|
18 |
|
19 | const app = express();
|
20 |
|
21 | app.use('/', express.static(appRootPathDist));
|
22 |
|
23 | app.use(bodyParser.json());
|
24 | app.use(morgan('dev'));
|
25 | app.use(cors);
|
26 |
|
27 | if (config['staticFolders']){
|
28 | config.staticFolders.forEach(folder => {
|
29 | if (typeof(folder) === 'string'){
|
30 | app.use(`/${folder}`, express.static(appRootPath(folder)));
|
31 | } else {
|
32 | app.use(`/${folder.route}`, express.static(appRootPath(folder.path)));
|
33 | }
|
34 | });
|
35 | }
|
36 |
|
37 | config.proxyServers.forEach(proxyServer => proxy(proxyServer, app));
|
38 |
|
39 | routes(app, appRootPath('api'));
|
40 |
|
41 | app.all('/*', (req, res) => res.sendFile('index.html', { root: appRootPathDist }));
|
42 |
|
43 | return {
|
44 | start () {
|
45 | const server = createServer(app);
|
46 | server.listen(config.port, config.ip)
|
47 | .on('listening', () => {
|
48 | const { port, address } = server.address();
|
49 | console.log(`Express server started on port ${port} at ${address}.`);
|
50 | });
|
51 | }
|
52 | }
|
53 |
|
54 | }
|
55 |
|
56 | module.exports = Server;
|