This repository has been archived by the owner on Sep 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathroutes.js
132 lines (112 loc) · 3.28 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @file: routes.js
* @author: youngsterxyf, gejiawen
* @date: 15/12/4 17:19
* @description: routes.js
*/
'use strict';
let fs = require('fs');
let urlParser = require('url');
//const qs = require('querystring');
let Parser = require('co-body');
let controllers = require('./controllers');
let StaticServ = require('./utils/staticServ');
let RespUtil = require('./utils/resp');
let Logger = require('./utils/logger');
// Web路由
let routes = {
'/': {
'verb': ['GET'],
'action': controllers.Home
},
'/cmd_page': {
'verb': ['GET'],
'action': controllers.Cmd_page
},
'/cmd': {
'verb': ['POST'],
'action': controllers.Cmd
},
'/stat_page': {
'verb': ['GET'],
'action': controllers.Stat_page
},
'/stat': {
'verb': ['POST'],
'action': controllers.Stat
}
};
function* _parseReqBody(req) {
let type = 'form';
let body;
let headers = req.headers;
if ('content-type' in headers) {
if (headers['content-type'] === 'application/json') {
type = 'json';
}
}
if (type === 'json') {
body = yield Parser.json(req);
} else {
body = yield Parser.form(req);
}
return body;
}
function _router(req, res) {
let urlParts = urlParser.parse(req.url, true);
let pathname = urlParts.pathname;
Logger.info('%s %s', req.method, pathname);
// 输出请求路径及方法
// console.log(pathname, req.method);
// 绑定一些方法
res.toResponse = RespUtil.toRespone;
// 匹配路由表
if ((pathname in routes) && (routes[pathname].verb.indexOf(req.method) != -1)) {
// 统一解析保存URL查询字符串的请求参数
// req.query = qs.parse(urlParts.query);
req.query = urlParts.query;
if (req.method === 'POST' || req.method === 'PUT') {
// 统一解析并保存请求体数据
_parseReqBody(req).next().value.then(function(parsedBody) {
req.body = parsedBody;
// console.log(req.body);
routes[pathname].action(req, res);
});
} else {
req.body = {};
// 执行对应的处理方法
routes[pathname].action(req, res);
}
return;
}
StaticServ(pathname, function(err, result) {
if (err) {
Logger.error(err);
res.toResponse(err.msg, err.code);
return;
}
if (result.cached) {
let data = result.data;
let statusCode = 200;
// 写响应头, 以及可能相应304
let ifNoneMatch = req.headers['if-none-match'];
if (ifNoneMatch && ifNoneMatch == data.md5) {
statusCode = 304;
}
res.statusCode = statusCode;
res.setHeader('Cache-Control', data.cacheControl || 'public, max-age=' + data.maxAge);
res.setHeader('ETag', data.md5);
res.setHeader('Expires', (new Date(Date.now() + data.maxAge * 1000)).toUTCString());
if (statusCode === 200) {
res.write(data.buffer);
}
} else {
res.write(result.data);
}
res.end();
});
}
/**
* Module Exports.
*/
module.exports = _router;