ohana
A http server that return simulation json data.
中文文档
Install
npm install ohana
Usage
You can use a variety data template parser. Recommendation: madoka and mockjs。
var madoka = require("madoka");
var Mock = require('mockjs');
var Server = require("ohana");
var server = new Server({
parser: madoka.generate
});
server.get('/article/', {
delay: 200,
data: function (params, query) {
console.log(params);
console.log(query);
return [
'{{ repeat(10) }}',
{
'id': '{{ index() }}',
'title': '{{ lorem(1, "sentences") }}',
'author': '{{ firstName() }} {{ lastName() }}',
'brief': '{{ lorem(1, "paragraphs") }}',
'post_time': '{{ date(new Date(2014, 0, 1), "YYYY-MM-dd HH:mm:ss") }}',
'read_count': '{{ integer(100, 10000) }}'
}
];
}
});
server.get('/article/:id', {
parser: Mock.mock,
data: function (params, query) {
return {
"status": "ok",
"data": {
"id": params.id,
"title": "@TITLE(5, 7)",
"author": "@NAME",
"post_time": "@DATETIME('yyyy-MM-dd HH:mm:ss')",
"content": "@PARAGRAPH(2)",
"poster": "@IMAGE('700x350', '#ccc', '#000', 'hello world')",
"read_count|0-1000": 100
}
}
}
});
server.get('/user/filter/', {
beforeResponse: function (data) {
return data.data;
},
data: function (params, query) {
return {
'data': [
'{{ repeat(5, 7) }}',
{
'user_name': '{{ firstName() }} {{ lastName() }}'
}
]
}
}
});
server.get('/user/:id/tag', {
contentType: 'text/html',
beforeResponse: function(data) {
return data.tag;
},
data: {
tag: 'loli'
}
});
server.start(3000);
API
Server(options)
Server constructor
server.get(path, options)
Listen a GET request.
path
: url pathoptions
:
parser
: template parserdelay
: delay response. Unit is ms.beforeResponse
: A function process data before responsecontentType
: response content-typestatusCode
: http status codedata
: data template. It can be a function. Function parameter 'params' matching routing parameters, 'query' is submitted or parameter query.
server.get('/article/:id', {
data: function (params, query) {
return {
"status": "ok",
"data": {
"id": params.id,
"title": "@TITLE(5, 7)",
"author": "@NAME",
"post_time": "@DATETIME('yyyy-MM-dd HH:mm:ss')",
"content": "@PARAGRAPH(2)",
"poster": "@IMAGE('700x350', '#ccc', '#000', 'hello world')",
"read_count|0-1000": 100
}
}
}
});
server.post(path, options)
POST request.
server.delete(path, options)
DELETE request.
server.put(path, options)
PUT request.
server.patch(path, options)
PATCH request.
server.proxy(path, option)
Proxy request.
path
: url pathoptions
:
urlRoot
: url root of target server,method
: request method,GET | POST | PUT | PATCH | DELETE
server.proxy('/article/:id', {
urlRoot: 'http://localhost:3000',
method: DELETE
});
var server = new Server({
proxy: {
urlRoot: 'http://localhost:3000',
method: 'get'
}
});
server.proxy('/article/');
server.proxy('/article/:id');
server.proxy('/article/', {
method: 'POST'
});
server.proxy('/article/:id', {
method: 'DELETE'
});
server.proxy('/article/:id', {
method: 'PUT'
});
server.proxy('/article/:id', {
method: 'PATCH'
});
server.register(apiList);
register module
server.register([
require('./article/index'),
require('./user/index')
]);
server.start(port, host)
Route
Basic string:
"/articles" will only match routes that == "/articles".
Named parameters:
"/articles/:title" will only match routes like "/articles/hello", but *not* "/articles/".
Optional named parameters:
"/articles/:title?" will match "/articles/hello" AND "/articles/"
Periods before optional parameters are also optional:
"/:n.:f?" will match "/1" and "/1.json"
Splaaaat! :
"/assets/*" will match "/assets/blah/blah/blah.png" and "/assets/".
"/assets/*.*" will match "/assets/1/2/3.js" as splats: ["1/2/3", "js"]
Mix splat with named parameters:
"/account/:id/assets/*" will match "/account/2/assets/folder.png" as params: {id: 2}, splats:["folder.png"]
Named RegExp:
"/lang/:lang([a-z]{2})" will match "/lang/en" but not "/lang/12" or "/lang/eng"
Raw RegExp:
/^\/(\d{2,3}-\d{2,3}-\d{4})\.(\w*)$/ (note no quotes, this is a RegExp, not a string.) will match "/123-22-1234.json". Each match group will be an entry in splats: ["123-22-1234", "json"]
visit: https://github.com/aaronblohowiak/routes.js
CH
ohana
一个返回模拟 json 数据的 http 服务器。
安装
npm install ohana
使用
可以使用多种 json 模板解析器。推荐使用 madoka 和 mockjs。
var madoka = require("madoka");
var Mock = require('mockjs');
var Server = require("ohana");
var server = new Server({
parser: madoka.generate
});
server.get('/article/', {
delay: 200,
data: function (params, query) {
console.log(params);
console.log(query);
return [
'{{ repeat(10) }}',
{
'id': '{{ index() }}',
'title': '{{ lorem(1, "sentences") }}',
'author': '{{ firstName() }} {{ lastName() }}',
'brief': '{{ lorem(1, "paragraphs") }}',
'post_time': '{{ date(new Date(2014, 0, 1), "YYYY-MM-dd HH:mm:ss") }}',
'read_count': '{{ integer(100, 10000) }}'
}
];
}
});
server.get('/article/:id', {
parser: Mock.mock,
data: function (params, query) {
return {
"status": "ok",
"data": {
"id": params.id,
"title": "@TITLE(5, 7)",
"author": "@NAME",
"post_time": "@DATETIME('yyyy-MM-dd HH:mm:ss')",
"content": "@PARAGRAPH(2)",
"poster": "@IMAGE('700x350', '#ccc', '#000', 'hello world')",
"read_count|0-1000": 100
}
}
}
});
server.get('/user/filter/', {
beforeResponse: function (data) {
return data.data;
},
data: function (params, query) {
return {
'data': [
'{{ repeat(5, 7) }}',
{
'user_name': '{{ firstName() }} {{ lastName() }}'
}
]
}
}
});
server.get('/user/:id/tag', {
contentType: 'text/html',
beforeResponse: function(data) {
return data.tag;
},
data: {
tag: 'loli'
}
});
server.start(3000);
API
Server(options)
Server构造函数
server.get(path, options)
匹配 GET 方式的请求。
path
: 路由匹配地址options
:
parser
: 数据解析器。delay
: 延迟多少毫秒后返回。beforeResponse
: 数据输出之前处理数据。contentType
: 响应数据的 content-typestatusCode
: http 状态码data
: 返回的数据,可以接受方法, 方法中的参数 params 是路由匹配的参数,query 是提交或查询的参数。
server.get('/article/:id', {
data: function (params, query) {
return {
"status": "ok",
"data": {
"id": params.id,
"title": "@TITLE(5, 7)",
"author": "@NAME",
"post_time": "@DATETIME('yyyy-MM-dd HH:mm:ss')",
"content": "@PARAGRAPH(2)",
"poster": "@IMAGE('700x350', '#ccc', '#000', 'hello world')",
"read_count|0-1000": 100
}
}
}
});
server.post(path, options)
与 get 同理
server.delete(path, options)
与 get 同理
server.put(path, options)
与 get 同理
server.patch(path, options)
与 get 同理
server.proxy(path, option)
代理请求
path
: 路由匹配地址options
:
urlRoot
: 目标服务器请求根目录,method
: 请求类型,GET | POST | PUT | PATCH | DELETE
server.proxy('/article/:id', {
urlRoot: 'http://localhost:3000',
method: DELETE
});
var server = new Server({
proxy: {
urlRoot: 'http://localhost:3000',
method: 'get'
}
});
server.proxy('/article/');
server.proxy('/article/:id');
server.proxy('/article/', {
method: 'POST'
});
server.proxy('/article/:id', {
method: 'DELETE'
});
server.proxy('/article/:id', {
method: 'PUT'
});
server.proxy('/article/:id', {
method: 'PATCH'
});
server.register(apiList);
注册 api
server.register([
require('./article/index'),
require('./user/index')
]);
server.start(port, host)
路由匹配规则:
字符串:
"/articles" 只会匹配到 "/articles"。 /articles/ 不会被匹配到。
命名参数:
"/articles/:title" 只会匹配到像"/articles/hello"的路由, 但是不会匹配到 "/articles/".
可选参数:
"/articles/:title?" 匹配 "/articles/hello" 和 "/articles/"。
可选参数前面的参数也是可选的:
"/:n.:f?" 会匹配到 "/1" 和 "/1.json"
星号(splat):
"/assets/*" 匹配到 "/assets/blah/blah/blah.png" 和 "/assets/".
"/assets/*.*" 会匹配到 "/assets/1/2/3.js", 并且有 splats: ["1/2/3", "js"]
星号和命名参数:
"/account/:id/assets/*" 会匹配到 "/account/2/assets/folder.png",并且有 params: {id: 2}, splats:["folder.png"]
正则表达式:
"/lang/:lang([a-z]{2})" 会匹配到 "/lang/en", 但是不会匹配到 "/lang/12" 和 "/lang/eng"
参考: https://github.com/aaronblohowiak/routes.js