servermock
node static server and mock data
version 1.0.15
fix 中文 path
version 1.0.14
增加pagemock data file config
version 1.0.11
+增加插件支持 support plugin
+pagemock plugin
pagemock now support:
most velocity template grammar
bits of php grammar like
<?php echo is_null($variable)? "": "<em>".$variable. "</em>元"; ?>
<?php echo $var; ?>
support most function, variable and bits of logic expression
Install
sudo npm install servermock -g
//启动命令(start)
servermock start
//or use as node module
npm install servermock --save
require('servermock')
Config
sm.config josn file in the project root or start path
{
"port": 8080,
"protocol": "http", //https
"key": "~/cert/cert.key", // if https
"cert": "~cert/cert.crt", // for https
"main": "index.html", // relative to root such http://127.0.0.1:8080/index.html
// mock请求
"mock":{
"datapath": "mock/",
"pagepath": "", //page mock data path, default same as page file with .json or .mjson
"mockrc": ".mockrc", //如果不是绝对路径则相对mock datapath
"ignore": ["jpg", "png", "gif", "html", "js", "css"], //default value
"regexurl": { //前面是regex new RegExp()
"/api/mockdata1" : "mockdata.js", //走js 遵循cmd
"/api/mockdata1" : "mockdata.json", //json数据返回
"/api/mockdata" : "mockdata.mjson" //mockjson数据返回
}
},
}
sm.config标准json 使用时去掉文件中去掉注释
the file is standard json widthout comment
protocol: http/https
when https you should give the value of key and cert
mock.datapath is the mock data root
mock.pagepath is page mock data path, default same as page file with .json or .mjson extname
mock.regexurl{name:value}
name: match mock url, support regex
value: match data file path relative datapath
Directory
builddir or start server directory
–mock/
—-.mockrc
—-mockdata.js
—-mockdata.json
—-mockdata.mjson
—-…
–src
–sm.config
or
var servermock = require('servermock');
servermock(config); // or use default config
Mock
support mockjson(.mjson) json(.json) function(req, res)(.js)
.js
function(req, res){
// req.headers: {
// host: '127.0.0.1:8080',
// connection: 'keep-alive',
// accept: 'application/json, text/javascript, */*; q=0.01',
// 'x-requested-with': 'XMLHttpRequest',
// 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',
// referer: 'http://127.0.0.1:8080/src/export/pages/app-1.html',
// 'accept-encoding': 'gzip, deflate, sdch',
// 'accept-language': 'zh-CN,zh;q=0.8,en;q=0.6,tr;q=0.4,ja;q=0.2',
// cookie: '_ga=GA1.1.412069457.1440574551',
// 'ra-ver': '3.0.7',
// 'ra-sid': 'D397FW#5-20150603-021623-afsadf-asfew'
// }
// req.method: 'GET',
// req.wwwurl: {
// protocol: 'http:',
// slashes: true,
// auth: null,
// host: '127.0.0.1:8080',
// port: '8080',
// hostname: '127.0.0.1',
// hash: null,
// search: '?city=%E5%8C%97%E4%&query=d',
// query: 'city=%E5%8C%97%E4%&query=d',
// pathname: '/api/placesuggestion',
// path: '/api/placesuggestion?city=%E5%8C%97%E4%&query=d',
// href: 'http://127.0.0.1:8080/api/placesuggestion?city=%E5%8C%97%E4%&query=d'
// queryObj:{
// city: '北京市',
// query: 'd'
// }
// }
// 对.js .css等重定向等
// res.statusCode = 302;
// res.setHeader("Location", "http://127.0.0.1:8088" + req.url);
// res.end();
console.log("req:", req);
//console.log("res:", res);
var data = {"errno":0,"data":[1,2,3,4,5,6,7,8,'a','b','c','d']};
data['data'] = data['data'].slice(Math.random(1)*8)
res.end(JSON.stringify(data)); //response.write(); response.end()
}
.json
{
"errno": 0,
"data": [
{
"id": 1,
"name": "shalles"
},{
"id": 2,
"name": "shalles2"
},{
"id": 3,
"name": "shalles3"
}
]
}
.mjson
{
"errno": 0,
"data|1-10": [{
"uid|0-1000": 1,
"name": "@MNAME",
"age|10-99": 0,
"city": "@MCITY"
}]
}
generate
data [1, 10]
uid betoween 0 and 1000
age betoween 10 and 99
random MNAME in .mockrc “MNAME”: [“shalles”, “东阳”, “小明”, “小梅”, “乔治”]
so name is one of [“shalles”, “东阳”, “小明”, “小梅”, “乔治”], so as city
more [ http://mockjs.com/#语法规范 ] (http://mockjs.com/#语法规范)
.mockrc自定义mockjson随机变量
除了默认的还提供自定义随机变量(*.mjson)
{
"MCITY":["北京", "上海", "广州", "深圳", "贵阳", "厦门", "成都"],
"MNAME": ["shalles", "东阳", "小明", "小梅", "乔治"]
}
test file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>servermock test demo</title>
</head>
<body>
<h2>show servermock server and mock data commond+R or F5 refresh this page</h2>
<div id="msg"></div>
<script src="./zepto.min.js"></script>
<script>
$.ajax({
url: '/com/api/mockdata.do',
dataType: 'json',
success:function(data){
if(data['errno'] === 0){
msg.innerHTML = JSON.stringify(data["data"]);
console.log(data["data"])
}
},
error:function(data){
alert('error' + JSON.stringify(data));
}
})
</script>
</body>
</html>
page mock
Directory
builddir
…
–pagedir
—-page1.php
—-page1.json / page1.mjson
—-page2.vm
—-page2.json / page2.mjson
—-…
–…
such
page1.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP page</title>
</head>
<body>
<div><span>name:</span> <?php $name ?></div>
<div><span>age:</span> <?php $age; ?></div>
<?php echo "show your info";?>
<br>
<?php echo is_null($show): "default": $show;?>
<div>
<?php echo is_null($variable)? "": "<em>".$variable. "</em>元"; ?>
</div>
</body>
</html>
page1.json
{
"$name": "shalles",
"$age": 18,
"show": null
}
page1.mjson
{
"name": "shalles",
"age|18-20": 18,
"show|0-1": "I will support php, python, jsp and so on"
}