mock.js
1.0 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
48
49
50
51
52
var urlencode = require('urlencode');
var mockData = require('../json/mockDate.json');
exports=module.exports=function(logger,rest,settings){
function getJson(url,method){
var backData={};
for(var i=0;i<mockData.length;i++){
var urlRegex=new RegExp(mockData[i].url,'g');
if(urlRegex.test(url)){
backData=mockData[i][method+'_data'];
return backData;
}
}
return backData;
}
function get(req,res,next){
res.send(getJson(req.path,'get'));
}
function post(req,res,next){
res.send(getJson(req.path,'post'));
}
function put(req,res,next){
res.send(getJson(req.path,'put'));
}
function patch(req,res,next){
res.send(getJson(req.path,'patch'));
}
function head(req,res,next){
res.send(getJson(req.path,'head'));
}
function del(req,res,next){
res.send(getJson(req.path,'del'));
}
return {
'get':get,
'post':post,
'put':put,
'patch':patch,
'head':head,
'delete':del
};
};
exports['@singleton']=true;
exports['@require']=['igloo/logger','utils/rest','igloo/settings'];