Skip to content

Commit 6b5c76e

Browse files
committed
Merge pull request #1 from Allenice/dev
update to v0.0.5 添加 register 方法和请求日志
2 parents e47a148 + 4d08302 commit 6b5c76e

File tree

5 files changed

+104
-18
lines changed

5 files changed

+104
-18
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ohana
33
一个返回模拟 json 数据的 http 服务器
44
特点:
55

6+
67
- 使用 mockjs 生成 json 数据
78
- 支持路由规则
89
- 可跨域访问
@@ -53,6 +54,25 @@ server.listen(3000);
5354
- options:
5455
- delay: 延迟多少毫秒后返回,
5556
- data: 返回的数据,可以接受对象和方法, 方法中的参数 params 是路由匹配的参数,query 是提交或查询的参数。
57+
58+
```javascript
59+
server.get('/article/:id', {
60+
data: function (params, query) {
61+
return {
62+
"status": "ok",
63+
"data": {
64+
"id": params.id,
65+
"title": "@TITLE(5, 7)",
66+
"author": "@NAME",
67+
"post_time": "@DATETIME('yyyy-MM-dd HH:mm:ss')",
68+
"content": "@PARAGRAPH(2)",
69+
"poster": "@IMAGE('700x350', '#ccc', '#000', 'hello world')",
70+
"read_count|0-1000": 100
71+
}
72+
}
73+
}
74+
});
75+
```
5676

5777
### server.post(path, options)
5878
与 get 同理
@@ -63,7 +83,19 @@ server.listen(3000);
6383
### server.patch(path, options)
6484
与 get 同理
6585

66-
### server.listen(port, host)
86+
### server.register(apiList);
87+
注册 api
88+
89+
- apiList: api 模块列表
90+
91+
```javascript
92+
server.register([
93+
require('./article/index'),
94+
require('./user/index')
95+
]);
96+
```
97+
98+
### server.start(port, host)
6799
- port: 服务器监听的网络端口
68100
- host: 主机
69101

example/app.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
var Server = require("../index.js");
3-
var article = require('./article/index');
43

54
var server = new Server();
65

7-
article(server);
6+
server.register([
7+
require('./article/index'),
8+
require('./user/index')
9+
]);
810

9-
server.listen(3000);
11+
server.start(3000);

example/user/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* user 列子
3+
* -- 路由规则:https://github.com/aaronblohowiak/routes.js
4+
* -- 数据模板文档:http://mockjs.com/
5+
* */
6+
7+
module.exports = function (server) {
8+
// 获取用户列表
9+
server.get('/user/', {
10+
delay: 200,
11+
data: function(params, query) {
12+
console.log(params);
13+
console.log(query);
14+
15+
return {
16+
"status": "ok",
17+
"total_count": 100,
18+
"data|10": [
19+
{
20+
"id|1-10000": 1,
21+
"user_name": "@NAME",
22+
"gender|1": ['male', 'female']
23+
}
24+
]
25+
}
26+
}
27+
});
28+
29+
}

index.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,49 @@ Server.prototype = {
7474
});
7575
},
7676

77+
/**
78+
* 注册 api
79+
* @param {Array[Function]} apiList - api 模块列表
80+
*/
81+
register: function (apiList) {
82+
var self = this;
83+
84+
apiList.forEach(function (api) {
85+
if(typeof api === 'function') {
86+
api(self);
87+
}
88+
});
89+
},
90+
7791
/**
7892
* create http server
7993
* @param port
80-
* @param host
94+
* @param [host]
8195
*/
82-
listen: function (port, host) {
96+
start: function (port, host) {
8397
port = port || 8080;
8498

8599
http.createServer(function (req, res) {
86-
var path = url.parse(req.url).pathname;
100+
101+
var path = url.parse(req.url).pathname,
102+
statusCode,
103+
date = new Date(),
104+
dateString = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.toLocaleTimeString();
87105

88106
// 查找匹配的路由
89-
var match = router.match(req.method + path);
90-
var header = {
91-
'Content-Type': 'application/json; charset=utf-8',
92-
'Access-Control-Allow-Origin': '*',
93-
'Access-Control-Allow-Methods': 'POST, GET, DELETE, PUT, PATCH'
94-
};
107+
var match = router.match(req.method + path),
108+
header = {
109+
'Content-Type': 'application/json; charset=utf-8',
110+
'Access-Control-Allow-Origin': '*',
111+
'Access-Control-Allow-Methods': 'POST, GET, DELETE, PUT, PATCH'
112+
};
95113

96114
if(match) {
97-
// 允许跨域访问
98-
res.writeHead(200, header);
115+
statusCode = 200;
116+
console.log(req.method, statusCode, req.url, dateString);
99117

118+
// 允许跨域访问
119+
res.writeHead(statusCode, header);
100120
if(req.method === 'GET') {
101121
match.query = url.parse(req.url, true).query || {};
102122
match.fn(req, res, match);
@@ -113,10 +133,13 @@ Server.prototype = {
113133
});
114134
}
115135
} else {
116-
res.writeHead(404, header);
117-
res.end(JSON.stringify({status: 404}));
136+
statusCode = 404;
137+
console.log(req.method, statusCode, req.url, dateString);
138+
res.writeHead(statusCode, header);
139+
res.end(JSON.stringify({status: statusCode}));
118140
}
119141

142+
120143
}).listen(port, host);
121144

122145
console.log('Server running at http://'+ (host||'localhost') +':'+ port +'/');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ohana",
3-
"version": "0.0.3",
3+
"version": "0.0.5",
44
"description": "A simple server that return fake json data for test",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)