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 = ;var Mock = ;var Server = ; // use 'madoka.generate' as default parservar server = parser: madokagenerate; // get article parserserver; // get one article, use mock.js to parse tempate.server; // process data before response.server; // specify content-type// return 'text/html' server; serverstart3000;
API
Server(options)
Server constructor
-
options
parser
: default parser. Every function like below can use as a parser.
{// parse tempate logicvar data = ;// return the parsed data.return data;}contentType
: default content-typeonError
: handle errorproxy
: default proxy request configurlRoot
: the root url of the target servermethod:
request method,GET | POST | PUT | PATCH | DELETE
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;
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; // global configvar server = proxy: urlRoot: 'http://localhost:3000' method: 'get' ; // use global configserver; server; server; server; server; server;
server.register(apiList);
register module
apiList
: api module list
server;
server.start(port, host)
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 = ;var Mock = ;var Server = ; // 使用 madoka.generate 为默认的解析器,解析数据模板var server = parser: madokagenerate; // 获取文章列表server; // 获取一篇文章, 使用 mock.js 解析server; // 输出数据之前,处理一下数据server; // 指定输出的 content-type// 返回 text/html 类型 server; serverstart3000;
API
Server(options)
Server构造函数
-
options
parser
: 默认数据模板解析器。任何像以下处理的方法都可以作为解析器
{// 解析数据模板的逻辑var data = ;// 返回解析后的数据return data;}contentType
: 默认输出数据的 content-typeonError
: 错误处理proxy
: 代理请求参数urlRoot
: 目标服务器请求根目录,method:
请求类型,GET | POST | PUT | PATCH | DELETE
server.get(path, options)
匹配 GET 方式的请求。
path
: 路由匹配地址options
:parser
: 数据解析器。delay
: 延迟多少毫秒后返回。beforeResponse
: 数据输出之前处理数据。contentType
: 响应数据的 content-typestatusCode
: http 状态码data
: 返回的数据,可以接受方法, 方法中的参数 params 是路由匹配的参数,query 是提交或查询的参数。
server;
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; // 可以设置全局默认配置var server = proxy: urlRoot: 'http://localhost:3000' method: 'get' ; // 之后可以省略配置server; server; server; server; server; server;
server.register(apiList);
注册 api
apiList
: api 模块列表
server;
server.start(port, host)
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"