您的位置:首页 > 理论基础 > 计算机网络

移动端网络调试 基于express的JsServerDemo

2017-06-20 10:38 405 查看
Demo链接地址 https://github.com/AllenCoder/JsServer
用途 作为自己mock测试服务器

可以简单的模拟各种请求数据

可以结合Charles 拦截代理,将线上环境的数据完整替换为本地的数据,可以方便模拟调试各种情形

1. 启动mock的JsServer

node .\bin\www


2. 用法 结合Charles

Charles 的 Map 功能分 Map Remote 和 Map

Local两种,前者是将制定的网络请求重定向到另一个网址,MapLocal 是将指定的网络请求重定向到本地文件。



. 本次直接使用charles的MapRemote功能,将charles 配置map Remote 配置参考如下:



http://115.159.24.246:8080/JsServertest.json



mock修改成本地的 数据 ,区别在于修改了本地字段 password 为:JsServerData



router.all('/test', function (req, res, next) {

console.log(req)

res.set({
'Content-Length': '123',
})

res.sendFile('JsServertest.json', {root: path.join(__dirname, '../public/res')});
// res.render('index', { title: 'Express' });
})


结合以上步骤 可以方便将app内网络请求转换成任意自己想要修改的数据演示 修改本地的(public/res/JsServertest.json)原接口响应结果



postman请求
http://115.159.24.246:8080/JsServertest.json
返回数据已经被修改为



不仅仅是可以修改返回的response的body内容还可以根据需要任意修改返回的header信息

router.all('/trade/go', function (req, res) {
console.log(req.headers)

/*
HTTP/1.1 200 OK
Server nginx
Date   Mon, 19 Jun 2017 02:35:26 GMT
Cache-Control  no-store
Content-Type   text/xml;charset=UTF-8
Content-Encoding   gzip
Vary   Accept-Encoding
Pragma no-cache
Set-Cookie JSESSIONID=43E6672555D36EAB234DB20C53828DD0; Path=/; HttpOnly
THE-TIME   Monday, 19-Jun-2017 10:35:27 CST
Transfer-Encoding  chunked
Proxy-Connection   Keep-alive
*/
var date= new Date()
res.set({
'Content-Type': 'text/json;charset=UTF-8',
'Content-Length': '123',
'Date': date.toDateString()
})
res.sendFile('JsServertest.json', {root: path.join(__dirname, '../public/res')});

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息