forked from adam1985/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvajs_mock_remote.js
98 lines (86 loc) · 2.38 KB
/
dvajs_mock_remote.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
'use strict';
/**
mock 代码太累,直接代理线上或线下的数据
相关问题:https://github.com/dvajs/dva/issues/110
依赖:
npm install request
剩余问题:
表单相关未测试,如有麻烦自行修改发送的参数
*/
var http = require('http');
var url = require('url');
var path = require('path');
var requestInstance = require('request');
var request_host_offline = 'http://offline.baidu.com';
var request_host_online = 'http://baidu.com';
var request_host = request_host_online;
var base_param = {
app_id: 10001
};
var authInfo = {
'xxxx': 'xxxxx'
};
var setParams = function(url, params) {
var str = '';
for (var i in params) {
str += ('&' + i + '=' + encodeURIComponent(params[i]));
}
return url + (url.indexOf('?') > 0
? str
: ('?' + str.slice(1)));
};
var getRequestJar = function() {
var jar = requestInstance.jar();
var authStr = '';
for (var i in authInfo) {
authStr += i + '=' + authInfo[i] + '; '
}
var cookie = requestInstance.cookie(authStr);
jar.setCookie(cookie, request_host);
return jar;
};
var getRequestHeaders = function(request) {
var headers = Object.assign(request.headers);
headers.host = url.parse(request_host).host;
headers.referer = request_host;
headers['X-Requested-With'] = 'XMLHttpRequest';
return headers;
};
var generatorId = function() {
var index = 0;
return function() {
return++ index;
}
}();
module.exports = {
'/api/*' (request, response) {
var urlParse = url.parse(request.url);
var request_url = setParams(request_host + urlParse.path, base_param);
var index = generatorId();
console.log('【' + index + ', request:】' + request_url);
requestInstance({
method: request.method,
url: request_url,
body: request.body,
formData: request.formData,
jar: getRequestJar(),
headers: getRequestHeaders(request)
}, function(error, resp, body) {
console.log('【' + index + ',response:】' + resp.statusCode);
if (resp.statusCode === 200) {
var result;
try {
result = JSON.parse(body);
} catch (ex) {
result = {
statusCode: resp.statusCode,
responseBody: body
};
}
response.json(result);
} else {
response.json({statusCode: resp.statusCode, responseBody: body});
}
});
}
};