Skip to content

Commit 24ab885

Browse files
committed
feat: 优化路由的解析,优化错误的显示
1 parent 560275f commit 24ab885

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
node_modules/
2-
yarn.lock
2+
yarn.lock

lib/components/core/errorHandler.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ function getMessage(env, e) {
3131
}
3232

3333
if (env === 'development') {
34-
return util.inspect(e) + '\n' + (e.stack || '');
34+
return util.inspect(e);
35+
}
36+
37+
if (e.code === 'ERR_ASSERTION') {
38+
return e.message;
3539
}
3640

3741
return 'Internel Server Error';

lib/components/router/Router.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ module.exports = class Router {
1616
* 目前路由匹配是使用`path-to-regexp`这个库来实现的
1717
* 它可以很方便地将路径样式的字符串转换成正则表达式
1818
*
19-
* @param {String|Object} rule - 定位的规则
19+
* @param {Object} rule - 定位的规则
2020
* @param {Object} options - 额外的选项
21-
* - method {String|Array} 只允许指定method
21+
* - method {String|Array} 指定method
2222
*/
2323
add(pattern, rule, options = {}) {
2424
const keys = [];
@@ -28,7 +28,6 @@ module.exports = class Router {
2828
return rNum.test(name) ? null : { index: index + 1, name };
2929
}).filter(v => v);
3030

31-
rule = typeof rule === 'string' ? parseRule(rule) : rule;
3231
const item = { regexp, queries, rule, options };
3332
debug('add %o', item);
3433
this.routes.push(item);
@@ -87,10 +86,6 @@ function verifyRequest(item, request) {
8786
return method.indexOf(current) !== -1;
8887
}
8988

90-
function parseRule(rule) {
91-
const parts = rule.split('#');
92-
return { module: parts[0], action: parts[1] };
93-
}
9489

9590
function createRouteInfo(item, match) {
9691
const { rule, queries } = item;
@@ -100,5 +95,5 @@ function createRouteInfo(item, match) {
10095
}, {});
10196

10297
const query = rule.query ? { ...paramQuery, ...rule.query } : paramQuery;
103-
return { module: rule.module, action: rule.action, query };
98+
return { ...rule, query };
10499
}

lib/components/router/parse.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ module.exports = function parse(fn) {
3535
* ...
3636
*/
3737
VERBS.forEach(verb => {
38-
router[verb] = function(match, to) {
38+
router[verb] = function(match, to, options) {
3939
const item = {
4040
match: withStateMatch(states, match),
41-
to: withStateOptions(states, parseRule(to)),
41+
to: withStateOptions(states, parseRule(to, options)),
4242
verb: verb
4343
};
4444
routes.push(item);
@@ -140,12 +140,12 @@ module.exports = function parse(fn) {
140140

141141

142142
const rRule = /^([^#:]+)[#:](.+)$/;
143-
function parseRule(rule) {
143+
function parseRule(rule, options) {
144144
if (typeof rule === 'string') {
145145
const match = rRule.exec(rule);
146146
rule = { module: match[1], action: match[2] };
147147
}
148-
return rule;
148+
return { ...rule, ...options };
149149
}
150150

151151

0 commit comments

Comments
 (0)