Is there a way to make this on a single function call?
是否有一种方法可以在单个函数调用中实现此功能?
var todo = function (req, res){};
app.get("/", todo);
app.get("/blabla", todo);
app.get("/blablablabla", todo);
Something like:
喜欢的东西:
app.get("/", "/blabla", "/blablablabla", todo );
I know this is a syntax mess, but just for giving an idea of what I would like to achieve, an array of the routes would be awesome!
我知道这是一个语法混乱,但仅仅为了让我知道我想要实现什么,一个路由数组将是了不起的!
Anyone know how to do this?
有人知道怎么做吗?
6 个解决方案
#1
58
I came across this question while looking for the same functionality.
我在寻找相同的功能时遇到了这个问题。
@Jonathan Ong mentioned in a comment above that using arrays for paths is deprecated but it is explicitly described in Express 4, and it works in Express 3.x. Here's an example of something to try:
@Jonathan Ong在上面的评论中提到,不赞成使用数组作为路径,但是在Express 4中有明确的描述,它在Express 3.x中工作。这里有一个可以尝试的例子:
app.get(
['/test', '/alternative', '/barcus*', '/farcus/:farcus/', '/hoop(|la|lapoo|lul)/poo'],
function ( request, response ) {
}
);
From inside the request
object, with a path of /hooplul/poo?bandle=froo&bandle=pee&bof=blarg
:
来自请求对象内部,路径为/hooplul/poo?bandle=froo&bandle=pee&bof=blarg:
"route": {
"keys": [
{
"optional": false,
"name": "farcus"
}
],
"callbacks": [
null
],
"params": [
null,
null,
"lul"
],
"regexp": {},
"path": [
"/test",
"/alternative",
"/barcus*",
"/farcus/:farcus/",
"/hoop(|la|lapoo|lul)/poo"
],
"method": "get"
},
Note what happens with params: It is aware of the capture groups and params in all of the possible paths, whether or not they are used in the current request.
注意params会发生什么:它知道捕获组和所有可能路径中的params,无论它们是否在当前请求中使用。
So stacking multiple paths via an array can be done easily, but the side-effects are possibly unpredictable if you're hoping to pick up anything useful from the path that was used by way of params or capture groups. It's probably more useful for redundancy/aliasing, in which case it'll work very well.
因此,通过数组来堆叠多条路径是很容易的,但是如果您希望从解析或捕获组使用的路径中获取任何有用的内容,那么副作用可能是不可预测的。它可能对冗余/混叠更有用,在这种情况下它会工作得很好。
Edit: Please also see @c24w's answer below.
编辑:请见下面@c24w的答案。
Edit 2: This is a moderately popular answer. Please keep in mind that ExpressJS, as with most Node.js libraries, is a moveable feast. While the routing above does still work (I'm using it at the moment, a very handy feature), I cannot vouch for the output of the request object (it's certainly different from what I've described). Please test carefully to ensure you get the desired results.
编辑2:这是一个比较流行的答案。请记住,与大多数节点一样,ExpressJS。js库是一个可移动的盛宴。虽然上面的路由仍然可以工作(我现在正在使用它,这是一个非常方便的特性),但是我不能保证请求对象的输出(它肯定与我描述的不同)。请仔细测试,以确保得到预期的结果。
#2
49
app.get('/:var(bla|blabla)?', todo)
:var
sets the req.param
that you don't use. it's only used in this case to set the regex.
:var设置要求。你不用的参数。它只用于设置regex。
(bla|blabla)
sets the regex to match, so it matches the strings bla
and blablah
.
(bla|blabla)设置regex匹配,因此它匹配字符串bla和blablah。
?
makes the entire regex optional, so it matches /
as well.
吗?使整个regex是可选的,因此它也匹配。
#3
44
You can actually pass in an array of paths, just like you mentioned, and it works great:
你可以通过一系列路径,就像你提到的那样,效果很好:
var a = ['/', '/blabla', '/blablablabla'];
app.get(a, todo);
#4
19
Just to elaborate on Kevin's answer, this is from the 4.x docs:
来详细解释一下凯文的答案,这是来自4。x文档:
The path for which the middleware function is invoked; can be any of:
调用中间件功能的路径;可以是任何:
- A string representing a path.
- 表示路径的字符串。
- A path pattern.
- 一个路径模式。
- A regular expression pattern to match paths.
- 匹配路径的正则表达式模式。
- An array of combinations of any of the above.
- 上述任何一种组合的组合。
They have some examples, including:
他们有一些例子,包括:
This will match paths starting with
/abcd
,/xyza
,/lmn
, and/pqr
:这将匹配从/abcd、/xyza、/lmn和/pqr开始的路径:
app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) { next(); });
#5
4
I went for a:
我去:
['path', 'altPath'].forEach(function(path) {
app.get(path, function(req, res) { etc. });
});
#6
0
require the file of your original route and define the new route like this
要求原始路由的文件,并像这样定义新的路由
var user = require('./users');
router.post('/login', user.post('/login'));
#1
58
I came across this question while looking for the same functionality.
我在寻找相同的功能时遇到了这个问题。
@Jonathan Ong mentioned in a comment above that using arrays for paths is deprecated but it is explicitly described in Express 4, and it works in Express 3.x. Here's an example of something to try:
@Jonathan Ong在上面的评论中提到,不赞成使用数组作为路径,但是在Express 4中有明确的描述,它在Express 3.x中工作。这里有一个可以尝试的例子:
app.get(
['/test', '/alternative', '/barcus*', '/farcus/:farcus/', '/hoop(|la|lapoo|lul)/poo'],
function ( request, response ) {
}
);
From inside the request
object, with a path of /hooplul/poo?bandle=froo&bandle=pee&bof=blarg
:
来自请求对象内部,路径为/hooplul/poo?bandle=froo&bandle=pee&bof=blarg:
"route": {
"keys": [
{
"optional": false,
"name": "farcus"
}
],
"callbacks": [
null
],
"params": [
null,
null,
"lul"
],
"regexp": {},
"path": [
"/test",
"/alternative",
"/barcus*",
"/farcus/:farcus/",
"/hoop(|la|lapoo|lul)/poo"
],
"method": "get"
},
Note what happens with params: It is aware of the capture groups and params in all of the possible paths, whether or not they are used in the current request.
注意params会发生什么:它知道捕获组和所有可能路径中的params,无论它们是否在当前请求中使用。
So stacking multiple paths via an array can be done easily, but the side-effects are possibly unpredictable if you're hoping to pick up anything useful from the path that was used by way of params or capture groups. It's probably more useful for redundancy/aliasing, in which case it'll work very well.
因此,通过数组来堆叠多条路径是很容易的,但是如果您希望从解析或捕获组使用的路径中获取任何有用的内容,那么副作用可能是不可预测的。它可能对冗余/混叠更有用,在这种情况下它会工作得很好。
Edit: Please also see @c24w's answer below.
编辑:请见下面@c24w的答案。
Edit 2: This is a moderately popular answer. Please keep in mind that ExpressJS, as with most Node.js libraries, is a moveable feast. While the routing above does still work (I'm using it at the moment, a very handy feature), I cannot vouch for the output of the request object (it's certainly different from what I've described). Please test carefully to ensure you get the desired results.
编辑2:这是一个比较流行的答案。请记住,与大多数节点一样,ExpressJS。js库是一个可移动的盛宴。虽然上面的路由仍然可以工作(我现在正在使用它,这是一个非常方便的特性),但是我不能保证请求对象的输出(它肯定与我描述的不同)。请仔细测试,以确保得到预期的结果。
#2
49
app.get('/:var(bla|blabla)?', todo)
:var
sets the req.param
that you don't use. it's only used in this case to set the regex.
:var设置要求。你不用的参数。它只用于设置regex。
(bla|blabla)
sets the regex to match, so it matches the strings bla
and blablah
.
(bla|blabla)设置regex匹配,因此它匹配字符串bla和blablah。
?
makes the entire regex optional, so it matches /
as well.
吗?使整个regex是可选的,因此它也匹配。
#3
44
You can actually pass in an array of paths, just like you mentioned, and it works great:
你可以通过一系列路径,就像你提到的那样,效果很好:
var a = ['/', '/blabla', '/blablablabla'];
app.get(a, todo);
#4
19
Just to elaborate on Kevin's answer, this is from the 4.x docs:
来详细解释一下凯文的答案,这是来自4。x文档:
The path for which the middleware function is invoked; can be any of:
调用中间件功能的路径;可以是任何:
- A string representing a path.
- 表示路径的字符串。
- A path pattern.
- 一个路径模式。
- A regular expression pattern to match paths.
- 匹配路径的正则表达式模式。
- An array of combinations of any of the above.
- 上述任何一种组合的组合。
They have some examples, including:
他们有一些例子,包括:
This will match paths starting with
/abcd
,/xyza
,/lmn
, and/pqr
:这将匹配从/abcd、/xyza、/lmn和/pqr开始的路径:
app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) { next(); });
#5
4
I went for a:
我去:
['path', 'altPath'].forEach(function(path) {
app.get(path, function(req, res) { etc. });
});
#6
0
require the file of your original route and define the new route like this
要求原始路由的文件,并像这样定义新的路由
var user = require('./users');
router.post('/login', user.post('/login'));