UNPKG

1.03 kBJavaScriptView Raw
1const path = require('path');
2
3const express = require('express');
4const morgan = require('morgan');
5const bodyParser = require('body-parser');
6
7const Server = (options = { port: 9028, host: "localhost", routeDir: path.resolve('server') }) => {
8 const public = path.join(options.routeDir, 'public');
9
10 const cors = require('../utils/cors');
11 const routes = require('../utils/app-route');
12
13 const http = require('http');
14 const app = express();
15
16 app.use('/', express.static(public));
17
18 app.use(bodyParser.json());
19 app.use(morgan('dev'));
20 app.use(cors);
21
22 routes(app, options.routeDir);
23
24 app.all('/*', (req, res) => res.sendFile('index.html', { root: public }));
25
26 return {
27 start () {
28 const server = http.createServer(app);
29 server.listen(options.port, options.host)
30 .on('listening', () => {
31 const { port, address } = server.address();
32 console.log(`Express server started on port ${port} at ${address}.`);
33 });
34 }
35 }
36};
37
38module.exports = Server;
\No newline at end of file