节点快速表达:您是否应该始终在get或post处理程序中调用next()? [英] node express: should you always call next() in a get or post handler?

查看:102
本文介绍了节点快速表达:您是否应该始终在get或post处理程序中调用next()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到现在,我仅使用(req, res)作为参数定义了getpost处理程序,并假设我将这些处理程序放在中间件链中的最后,并确保我处理了所有响应并在这些处理程序中正确地进行错误处理...因此,我没有对next进行任何引用都没关系.

Until now I've defined my get and post handlers with just (req, res) as arguments, with the assumption being that I put these handlers last in the chain of middleware, and make sure that I handle any responses and error handling properly within these handlers... hence it doesn't matter that I don't make any reference to next.

这是一种有效且明智的方法吗?或者即使现在(之后)什么都没有发生,总是打电话给next()是一种好习惯吗?例如,也许将来您可能希望在这些路线之后进行一些处理……或者也许由于某种原因,我还没有找到为什么总是调用next()的好习惯.

Is this a valid and sensible approach, or is it good practice always to call next() even if (at present) there is nothing coming afterwards? For example, perhaps in the future you might want to do some handling after these routes... or maybe there's a reason I haven't yet come across why it's good practice to always call next().

例如,在路由指南中有以下简单示例:

For example, there is the following simple example in the express routing guide:

app.get('/example/b', function (req, res, next) {
    console.log('the response will be sent by the next function ...')
    next()
}, function (req, res) {
    res.send('Hello from B!')
})

当然,我知道这是一个非常简单的示例,用于说明可以链接处理程序,并且不打算为get处理程序提供完整的框架,但是定义和使用甚至在第二个处理程序中,如下所示?

Of course, I appreciate that this is a very simple example to illustrate that handlers can be chained, and is not intended to provide a complete framework for a get handler, but would it be better to define and use next even in the second handler, as follows?

app.get('/example/b', function (req, res, next) {
    console.log('the response will be sent by the next function ...')
    next()
}, function (req, res, next) {
    res.send('Hello from B!')
    next()
})

还是实际上通常的做法是假设将响应发送回客户端的处理程序函数应该调用next() ...,即假设链应该终止于实际发送响应的处理程序?

Or is it actually common practice to assume that a handler function that sends a response back to the client should not call next()... i.e. the assumption should be that the chain will end at the handler that actually sends the response?

或者在这一点上没有确定的惯例吗?

Or is there no established practice on this point?

我什至想知道,是否不常见的是不在get处理程序中发送任何响应,而是将其推迟到之后的专用响应处理程序中……我所说的是OK响应处理程序,而不是错误响应处理程序(为此,通常定义最终错误处理程序并调用next(err)).因此,在非错误情况下,您将调用next(),并在以下中间件中执行res.status(200).send(req.mydata),其中req.mydata被添加到get处理程序中.

I'm even wondering whether it might be common not to send any response in the get handler but to defer that to a dedicated response handler coming after... by which I mean an OK response handler rather than an error response handler (for which it seems to be common practice to defined a final error handler and call next(err)). So, in a non-error situation, you would call next() and in the following middleware you would do your res.status(200).send(req.mydata) where req.mydata is added in your get handler.

推荐答案

否.如果您希望其他东西来处理请求,则仅应调用next().通常,这就像是说您的路线可能与该请求相匹配,但您想表现得不一样.例如,对于相同的路由,您可能有两个处理程序:

No. You should only call next() if you want something else to handle the request. Usually it's like saying that your route may match that request but you want to act like it didn't. For example you may have two handlers for the same route:

app.get('/test', (req, res, next) => {
  if (something) {
    return next();
  }
  // handle request one way (1)
});

app.get('/test', (req, res) => {
  // handle request other way (2)
});

始终会调用第一个匹配的处理程序,因此对于GET /test请求,将调用第一个处理程序,但是它可以选择将控件传递给第二个处理程序,就像第一个处理程序与请求不匹配一样.

Always the first matching handler is called, so for the GET /test request the first handler will be called, but it can choose to pass the control to the second handler, as if the first didn't match the request.

请注意,如果第二个处理程序不打算将请求传递给下一个处理程序,则其参数中甚至没有next.

Note that if the second handler doesn't intend to pass the request to the next handler, it doesn't even have next in its arguments.

如果没有第二个处理程序,则第一个处理程序称为next()时将使用标准的404处理程序.

If there was no second handler, then the standard 404 handler would be used if the first one called next().

如果将参数传递给next(),则将调用错误处理中间件.

If you pass an argument to next() then an error handling middleware will be called.

这篇关于节点快速表达:您是否应该始终在get或post处理程序中调用next()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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