Node / express,可选参数在get和post之间切换路由

时间:2022-09-06 11:13:12

I have 2 routes set up for my express server that look very close to each other. They are basically the same url, except one is post and one is get, and the get has an extra route param (which is optional). Right now these seem to work ok, however if I do not add the optional param to the get call, it thinks I'm trying to hit the post. I would like to be able to hit the get call without the passing the second optional param as well. Let me show you what I have so far:

我为我的快递服务器设置了2条路线,它们看起来非常接近。它们基本上是相同的url,除了一个是post,一个是get,get有一个额外的路由参数(这是可选的)。现在这些似乎工作正常,但是如果我不在get调用中添加可选参数,它会认为我正试图点击帖子。我希望能够在没有传递第二个可选参数的情况下点击获取调用。让我告诉你到目前为止我所拥有的:

router.param('itemID', (req, res, next, itemID) => {
    verbose("itemID=", itemID);
    next();
});

router.param('navigationType', (req, res, next, navigationType) => {
    if (!navigationType) {
        next();
    }
    verbose("navigationType=", navigationType);
    next();
}); 

router.route('/:itemID/navigations')
    .post(controllers.addActivity)
    .all(routes.send405.bind(null, ['POST']));

router.route('/:itemID/navigations/:navigationType')
    .get(controllers.listActivities)
    .all(routes.send405.bind(null, ['GET']));

The routed.send405 method looks like this :

routed.send405方法如下所示:

function send405(methods, req, res) {
    res.set('Allow', methods.join(','));
    res.status(405).json({
        message: `Method '${req.method}' Not Allowed.`
    });
}

So right now the issue is if I do a get on /blah123/navigations and don't add the /:navigationType variable, it thinks I am trying to hit the post method. I am very new to working with this and would appreciate any help or insight. Thanks!

所以现在的问题是,如果我做了一个get / blah123 / navigations并且不添加/:navigationType变量,它认为我试图点击post方法。我很擅长这项工作,并希望得到任何帮助或见解。谢谢!

1 个解决方案

#1


0  

When you declare a route, say GET /admins/:id, it will match any requests to GET /admins/1 or GET /admins/john. But when you do just GET /admins, it wouldn't be able to find because you haven't declared GET route matching that pattern.

当您声明路由时,请说GET / admins /:id,它将匹配对GET / admins / 1或GET / admins / john的任何请求。但是当你只做GET / admin时,它将无法找到,因为你没有声明GET路由匹配该模式。

To work with this, you have to specify navigationType is an optional parameter and also place the GET request first followed by the POST, like this.

要使用它,你必须指定navigationType是一个可选参数,并且首先放置GET请求,然后是POST,就像这样。

router.route('/:itemID/navigations/:navigationType?')
    .get(controllers.listActivities)
    .all(routes.send405.bind(null, ['GET'])); 

router.route('/:itemID/navigations')
    .post(controllers.addActivity)
    .all(routes.send405.bind(null, ['POST']));

#1


0  

When you declare a route, say GET /admins/:id, it will match any requests to GET /admins/1 or GET /admins/john. But when you do just GET /admins, it wouldn't be able to find because you haven't declared GET route matching that pattern.

当您声明路由时,请说GET / admins /:id,它将匹配对GET / admins / 1或GET / admins / john的任何请求。但是当你只做GET / admin时,它将无法找到,因为你没有声明GET路由匹配该模式。

To work with this, you have to specify navigationType is an optional parameter and also place the GET request first followed by the POST, like this.

要使用它,你必须指定navigationType是一个可选参数,并且首先放置GET请求,然后是POST,就像这样。

router.route('/:itemID/navigations/:navigationType?')
    .get(controllers.listActivities)
    .all(routes.send405.bind(null, ['GET'])); 

router.route('/:itemID/navigations')
    .post(controllers.addActivity)
    .all(routes.send405.bind(null, ['POST']));