-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (36 loc) · 1.17 KB
/
app.js
File metadata and controls
47 lines (36 loc) · 1.17 KB
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
const fs = require('fs');
const https = require('https');
const http = require('http');
const Koa = require('koa');
const cors = require('koa2-cors');
const debug = require('debug')('iplat');
const response = require('./middlewares/response');
const bodyParser = require('koa-bodyparser');
const config = require('./config');
const env = process.env.NODE_ENV;
// 同步读取密钥和签名证书
const options = {
key: fs.readFileSync('./server/index.key'),
cert: fs.readFileSync('./server/index.crt')
};
const app = new Koa();
const koaBody = require('koa-body');
app.use(koaBody({
multipart: true
}));
// 定义server
const httpsServer = https.createServer(options, app.callback());
const httpServer = http.createServer(app.callback());
// 跨域
app.use(cors());
// 使用响应处理中间件
app.use(response);
// 解析请求体
app.use(bodyParser());
// 引入路由分发
const router = require('./routes');
app.use(router.routes());
// https监听4000端口
httpsServer.listen(config[env].https, () => debug(`listening on port ${config[env].https}`));
// http监听4003端口
httpServer.listen(config[env].http, () => debug(`listening on port ${config[env].http}`));