-
Notifications
You must be signed in to change notification settings - Fork 2
/
fc.js
executable file
·62 lines (51 loc) · 1.45 KB
/
fc.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
/**
* fc for nirvana project
* @author liandong([email protected])
*
*/
var querystring = require('querystring');
/**
* getQuery Response data
* @param {Object} config {response: 'modPath'}
* @return {Function}
*
* please config the edp-webserver-config.js as
*
* {
* location: /\/request\.ajax/,
* handler: mock.getQuery({response: './resp'})
* }
*/
exports.getQuery = function(responsor) {
var me = this;
me.response = responsor;
function getContent(req) {
var path = req.query.path;
var params = req.body.params;
var rst = {};
//所有的请求,必有path和params两个字段
if (path && req.body && req.body.params) {
rst = me.response.getResult(path, params);
}
return rst;
}
return function( context ) {
var req = context.request;
context.header[ 'Content-Type' ] = 'application/json';
if (req.bodyBuffer) {
var buffer = req.bodyBuffer.toString();
req.body = querystring.parse(buffer);
if (req.body && req.body.params) {
req.body.params = JSON.parse(req.body.params);
}
}
else {
req.body = {};
}
var rst = getContent(req);
//context.content = JSON.stringify(rst, ' ', 3);
context.content = JSON.stringify(rst);
context.status = 200;
context.start();
};
};