-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
106 lines (89 loc) · 2.61 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const path = require('path');
const config = require('./config');
const express = require('express');
const bodyParser = require('body-parser');
const bodyParserJsonError = require('express-body-parser-json-error');
const morgan = require('morgan');
const cors = require('cors');
const app = express();
const {errorHandler} = require('./common/errorHandler');
const requestIp = require('request-ip');
app.use(requestIp.mw());
app.use(bodyParser.json({limit:"10mb"}));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParserJsonError());
app.use(cors({
origin: true,
methods: ['GET','POST', 'PUT', 'DELETE'],
allowedHeaders:['Content-Type','Authorization', 'uuid','email', 'Pragma']
}));
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
const rootPath = path.normalize(path.join(__dirname, './public'));
app.use(express.static(rootPath));
// swagger configurations
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
// config swagger-jsdoc
const swaggerOptions = {
definition: {
// open api version
openapi: '3.0.0',
// swagger pages
info: {
title: 'User blog',
version: '1.0.0',
},
components:{
securitySchemes:{
ApiKeyAuth:{
type: 'apiKey',
in: 'header',
name: 'Authorization'
},
}
}
},
// path to collect swagger comment
apis: [ path.join(__dirname, '/app/**/*.controller.js')]
};
// generate jsDoc
const swaggerSpec = swaggerJSDoc(swaggerOptions);
// open swagger api
app.get('/swagger.json', function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
// use swaggerSpec generate swagger document pages,and put it to specified router
app.use('/api/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// routes
const routes = require('./app/router');
app.use('/api/v1',routes);
// handle error
app.use(errorHandler);
// show api routers in console
let urls = [];
const paresHandler = require('./common/utils/parseHandler');
paresHandler('',urls,app._router);
urls.forEach(u=>{
console.info(u);
});
// add socket io server feature
const server = require('http').Server(app);
server.listen(config.httpPort, '0.0.0.0', () => {
console.log(`listening on http port: ${config.httpPort}`)
});
module.exports = {
app:app,
httpServer:server
};
// process.on('SIGINT', () => {
// console.log('Received SIGINT')
// // if (server) {
// // server.close(() => {
// // console.log('server close, exiting')
// // process.exit(0)
// // })
// // }
// })