-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (75 loc) · 2.76 KB
/
app.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
const express = require('express');
const router = express.Router();
const path = require('path')
const fs = require('fs');
const url = require('url');
const app = express();
var isDev = process.env.NODE_ENV !== 'production';
const proxy = require('./server/proxy.js');
//const tools = require('./lib/tools.js');
const ifProxy = true;
const proxyConfig = {
host: 'localhost', // 这里是代理服务器
port: 80 // 这里是代理服务器端口
}
//是否是开发模式
if (isDev) {
var mockPath = path.resolve(__dirname, './server/mockData');
var mockConfig = require('./mock.config.js');
const readFile = require('./server/readFile.js').readFile;
//设置静态资源目录
app.use(express.static(path.join(__dirname, 'dist')))
var setInterface = function(reqPath, type, dataPath) {
if (reqPath.indexOf("?") !== "-1") {
reqPath = reqPath.split("?")[0];
}
if (type === "get") {
app.get(reqPath, function(req, res) {
if (ifProxy) {
var params = req.query;
proxy(proxyConfig, reqPath, 'get', params, function(content) {
res.send(content);
});
} else {
var realPath = path.join(mockPath, dataPath);
var file = readFile(realPath, function(content) {
if (content !== false) {
res.send(content);
} else {
res.send("查询失败");
}
})
}
})
} else if (type === "post") {
app.post(reqPath, function(req, res) {
if (ifProxy) {
var params = req.query;
proxy(proxyConfig, reqPath, 'get', params, function(content) {
res.send(content);
});
} else {
var realPath = path.join(mockPath, dataPath);
var file = readFile(realPath, function(content) {
if (content !== false) {
res.send(content);
} else {
res.send("查询失败");
}
})
}
})
}
}
for (var i = 0, j = mockConfig.length; i < j; i++) {
setInterface(mockConfig[i].url, mockConfig[i].type, mockConfig[i].dataPath)
}
}
app.get('/', function(req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});