-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_apiserver.js
38 lines (35 loc) · 1.22 KB
/
_apiserver.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
/*引入express*/
var express = require('express');
var port = 8003;
var path = require('path');
/*实例化express*/
var app = express();
// app.use(express.static(path.join(__dirname, 'dist')));
process.env.NODE_ENV = 'production';
//设置跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
const mock = {}
require('fs').readdirSync(require('path').join(__dirname + '/src/mock')).forEach(function(file) {
Object.assign(mock, require('./src/mock/' + file))
})
for (let key in mock) {
let func = key.split(' ')[1]
let method = key.split(' ')[0]
/*设置监听端口,同时设置回调函数,监听到事件时执行回调函数*/
if (method == 'GET') {
app.get(func, mock[key]);
} else if (method == 'POST') {
app.post(func, mock[key]);
}
}
app.listen(port, function afterListen () {
console.log('---------------------------------------')
console.log('express running on the http://localhost:%s', port);
});