1 | 1 | const express = require('express'); |
|
2 | 1 | const bodyParser = require('body-parser'); |
|
3 | 1 | const path = require('path'); |
|
4 | 1 | const morgan = require('morgan'); |
|
5 | 1 | const cors = require('cors'); |
|
6 | |||
7 | 1 | const v1 = require("./v1/index.js"); |
|
8 | 1 | const v2 = require("./v2/index.js"); |
|
9 | |||
10 | 1 | const app = express(); |
|
11 | |||
12 | 1 | app.use(cors()); |
|
13 | 1 | app.options('*', cors()); |
|
14 | |||
15 | 1 | app.disable('x-powered-by'); |
|
16 | |||
17 | 1 | app.set("view engine", "ejs"); |
|
18 | |||
19 | 2 | const port = process.env.PORT || 8111; |
|
20 | |||
21 | // don't show the log when it is test |
||
22 | 2 | if (process.env.NODE_ENV !== 'test') { |
|
23 | // use morgan to log at command line |
||
24 | app.use(morgan('combined')); // 'combined' outputs the Apache style LOGs |
||
25 | } |
||
26 | |||
27 | 1 | app.use(bodyParser.json()); // for parsing application/json |
|
28 | 1 | app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded |
|
29 | |||
30 | 1 | app.use(express.static(path.join(__dirname, "public"))); |
|
31 | |||
32 | 1 | app.use("/v2", v2); |
|
33 | |||
34 | 1 | app.get('/', (req, res) => res.redirect('/v2')); |
|
35 | |||
36 | 1 | app.use("/", v1); |
|
37 | |||
38 | 1 | const server = app.listen(port, () => console.log('Order api listening on port ' + port)); |
|
39 | |||
40 | module.exports = server; |
||
41 |