express.js路由说明 [英] express.js routes explanation
问题描述
我正在查看 express.js 源代码,以了解如何将命名路由参数映射到 req.params
属性。
I was looking at express.js source code, to find out how it maps named route parameters to req.params
properties.
对于那些不知道的人,您可以在 express.js 中定义具有命名参数的路由,使其可选,只允许具体格式(和更多):
For those who don't know, in express.js you can define routes with named parameters, make them optional, only allow the ones with specific format (and more):
app.get("/user/:id/:name?/:age(\\d+)", function (req, res) {
console.log("ID is", req.params.id);
console.log("Name is", req.params.name || "not specified!");
console.log("Age is", req.params.age);
});
我意识到这个功能的核心是一种称为 pathRegexp()
在 lib / utils.js 。方法定义如下:
I realized that the heart of this functionality is a method called pathRegexp()
defined in lib/utils.js. The method definition is as follows:
function pathRegexp(path, keys, sensitive, strict) {
if (path instanceof RegExp) return path;
if (Array.isArray(path)) path = '(' + path.join('|') + ')';
path = path
.concat(strict ? '' : '/?')
.replace(/\/\(/g, '(?:/')
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function (_, slash, format, key, capture, optional, star) {
keys.push({ name: key, optional: !! optional });
slash = slash || '';
return ''
+ (optional ? '' : slash)
+ '(?:'
+ (optional ? slash : '')
+ (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
+ (optional || '')
+ (star ? '(/*)?' : '');
})
.replace(/([\/.])/g, '\\$1')
.replace(/\*/g, '(.*)');
return new RegExp('^' + path + '$', sensitive ? '' : 'i');
}
重要的部分是第7行正则表达式, /(\ /)?(\。 )?(\w +)(?:( \(。*?\)))?(\?)?(\ *)?/ g
路径名称:
The important part is the regex on line 7, /(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g
which groups the matched portions of pathname this way:
slash
符号   ;                        &NBSP ;                        &NBSP ;                        &NBSP ;                 &NBS p;
slash
the /
symbol
格式
我不知道目的是什么这个,需要解释。   ;
key
\w +
)在之后:
符号   ;                        &NBSP ;                        &NBSP ;
key
the word (ie. \w+
) after the :
symbol
捕获
在键前面写的正则表达式
。应该用括号括起来(例如(。\\d +)
)
capture
a regex written in front of the key
. Should be wrapped in parenthesis (ex. (.\\d+)
)
< kbd>可选
键后的
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; ?
符号
kbd>明星
*
符号 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBS磷;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
star
the *
symbol
并且回调处理程序从上面的组中构建正则表达式。
and the callback handler builds a regex from the groups above.
现在的问题是
我的理解如下:
(format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)'))
,所提到的正则表达式是
and the mentioned regex is,
如果你放一个
符号后面的斜杠
组,并且不指定匹配条件(正则表达式包含在键之后的括号中
),所生成的正则表达式与
路径
的其余部分匹配,直到它达到。
或 /
符号。
if you put a .
symbol after the slash
group and don't specify a match condition (the regex wrapped in parenthesis after the key
), the generated regex matches the rest of path
until it gets to a .
or /
symbol.
那么有什么意义?
我问这个是因为:
- 我想在我的应用程序,并希望在使用它之前充分了解它的工作原理。
- 我没有在 express.js 文档中找到任何内容。
- 我只是好奇:)
- I want to extract and use this method in my app and want to fully understand how it works before using it.
- I didn't find anything on express.js documentation about it.
- I'm just curious :)
推荐答案
给定路径'/ path /:file。:ext'
,考虑表达式之间的区别:
Given the path '/path/:file.:ext'
, consider the difference between the expressions:
// With 'format' checking
/^\/path\/(?:([^\/]+?))(?:\.([^\/\.]+?))\/?$/
// Without 'format' checking
/^\/path\/(?:([^\/]+?))(?:([^\/]+?))\/?$/
在第一种情况下,你结束作为
In the first case, you end up with params as
{
file: 'file',
ext: 'js'
}
但是没有格式检查,你最终可以这样:
but without the format checking, you end up with this:
{
file: 'f',
ext: 'ile.js'
}
这篇关于express.js路由说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!