express.js - 单行路由的单路由处理程序 [英] express.js - single routing handler for multiple routes in a single line

查看:104
本文介绍了express.js - 单行路由的单路由处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var todo = function(req,res){ }; 

app.get(/,todo);
app.get(/ blabla,todo);
app.get(/ blablablabla,todo);

如下所示:

  app.get(/,/ blabla,/ blablablabla,todo); 

我知道这是一个语法混乱,只是为了给出想要实现的想法,一系列路线会很棒!



任何人都知道如何做?

解决方案

这个问题在寻找相同的功能时。



@Jonathan Ong在上面的评论中提到,对于路径使用数组已被弃用,但在Express 4中已明确描述在Express 3.x中工作。以下是一些要尝试的例子:

  app.get(
['/测试','/替代','/ barcus *','/ farcus /:farcus /','/ hoop(| la | lapoo | lul)/ poo'],
函数(请求,响应){

}
);

请求路径 / hooplul / poo?bandle = froo& bandle = pee& bof = blarg

 route:{
keys:[
{
可选:false,
name:farcus
}
],
回调:[
null
],
params:[
null,
null,
lul
],
regexp:{},
path:[
/ test,
/ alternative,
/ barcus *,
/ farcus /:farcus /,
/ hoop(| la | lapoo | lul)/ poo
],
method:get
},

注意params会发生什么:意识到所有可能路径中的捕获组和参数,无论它们是否在当前请求中使用。



因此可以通过数组堆叠多个路径很容易,但t如果您希望从通过参数或捕获组的方式使用的路径中获取有用的东西,那么副作用可能是不可预测的。这可能对于冗余/别名更有用,在这种情况下它将会很好地工作。



编辑:另请参阅@ c24w的答案以下



编辑2:这是一个适度受欢迎的答案。请记住,ExpressJS与大多数Node.js库一样是可移动的盛宴。虽然上面的路由仍然工作(我目前使用它是一个非常方便的功能),我不能保证请求对象的输出(这和我所描述的肯定不同)。请仔细检查以确保您获得所需的结果。


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?

解决方案

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:

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:

"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.

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.

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.

这篇关于express.js - 单行路由的单路由处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆