在项目开发的过程当中,总会有前端开发快完成,后端接口却迟迟提供不了的情况。此时为了不影响前端开发的进度,我们可以借助puer来模拟后端接口测试。简单的说,puer就是一个可以实时刷新的前端服务器。具体关于puer的介绍可以参考这篇文章:http://leeluolee.github.io/2014/10/24/use-puer-helpus-developer-frontend/
1.先安装nodeJS
2.安装puer
npm install puer -g
3.puer命令介绍
Usage: puer [options...]
Options:
-p,--port server‘s listen port, 8000 default,可使用这个命令修改端口,比较常用
-f,--filetype fileType to watch(split with ‘|‘), default ‘js|css|html|xhtml‘
-d,--dir your customer working dir. default current dir
-i,--inspect start weinre server and debug all puer page
-x,--exclude exclude file under watching(must be a regexp), default: ‘‘
-a,--addon your addon‘s path,绑定域名,比较常用
-t,--target proxy url,代理URL,比较常用,默认是localhost
--no-reload close auto-reload feature,(not recommended)
--no-launch close the auto launch feature
-h,--help help list
4. 使用方法
4.1命令行中输入以下命令。(第一种方法),可使用这种方法快速编写静态页面,不需要手动刷新。
cd /path/to/workspace ↵
puer
puer会默认打开http://localhost:8000/(端口可以用 -p 8001参数进行控制),页面会列出该目录下所有的文件
4.2命令行中输入以下命令(第二种方法)
cd /path/to/workspace ↵
puer -a route.js
puer提供了叫插件(addon)的功能,集成了express的route.js(route.js的名字可以随意取)来达到最简的路由配置,可以提供基于真实http请求与相应的动态的mock数据。route.js文件代码如下:
// use addon to mock http request module.exports = { // GET "GET /v1/posts/:id": function(req, res, next){ // response json format res.send({ title: "title changed", content: "tow post hahahah", desc:‘aaaa‘, age:‘111‘, name:‘bbbb‘ }) }, // PUT POST DELETE is the same "PUT /v1/posts/:id": function(){ }, "POST /v1/posts": function(){ }, "DELETE /v1/posts/:id": function(){ } }
它其实就是一段nodejs程序,输出是一配置对象,key的空格前代表的是请求Method,后半部分是请求路径,而value代表回调函数和express的路由中间件是一致的,传入的是request和response对象。返回配置中的URL就可以返回定义的值,如下图:
原文:http://www.cnblogs.com/zourong/p/4924205.html