英文:
Nodejs Express, routes with multiple parameters in function?
问题
我通常看到Express路由如下,并对传递给app.get()
的多个参数有疑问。
我理解使用回调函数function(req, res)
的典型用法,但对回调函数具有多个参数或app.get()
接收多个参数的情况感到困惑。
标准且有文档支持的用法:
app.get('/', function(req, res){
});
但是,我也看到其他用法,带有多个参数:
app.get('/user/:id/edit', a, b, function(req, res){
});
我假设function a(req, res)
,function b(req, res)
,所以上面只是回调函数的列表。响应是按从左到右追加的吗?
在这种情况下,回调函数有一个额外的参数next
。Javascript编译器是否总是提供next()
,并且它是否只是在上面的处理程序中被忽略,因为它们没有第三个参数?
谢谢。
英文:
I typically see Express routes as follows and have questions about multiple parameters passed to app.get()
.
I understand typical usage with callback function function(req, res)
however am confused about the cases where the callback has multiple parameters or app.get()
receives multiple parameters.
Standard and documented:
app.get('/', function(req, res){
});
However, I also see other usage with multiple parameters:
app.get('/user/:id/edit', a, b, function(req, res){
});
I'm assuming function a(req, res)
, function b(req, res)
, so above is just a list of callbacks. Is the response just appended left to right?
app.get('/example/b', (req, res, next) => {
})
In this case the callback have an extra argument next
. Does the Javascript compiler always provide next()
and it's simply dropped in the above handlers where they have no third argument?
Thanks
答案1
得分: 0
app.get(path, callback [, callback ...])
(方括号[]
表示可选参数,省略号...
表示可重复参数)。
我假设function a(req, res)
,function b(req, res)
,所以上面只是一个回调函数列表。响应会按顺序从左到右追加。
这些函数会按照顺序从左到右调用。每个函数将会传递给它们通常的参数。
在这种情况下,回调函数多了一个额外的参数next
。JavaScript 编译器是否总是提供next()
,它只是在上述处理程序中没有第三个参数的情况下被丢弃?
这些参数是由 Express 的一部分函数传递的,而不是编译器本身。
任何不为参数注册参数的函数都会将其丢弃(大多数情况下;参见arguments 对象):
function foo(a, b, c) {
console.log(a + b);
}
foo(1, 2, 4, 8);
英文:
> However, I also see other usage with multiple parameters:
Also documented:
> app.get(path, callback [, callback ...])
(with the []
indicating optional arguments and the ...
indicating repeating ones).
> I'm assuming function a(req, res), function b(req, res), so above is just a list of callbacks. Is the response just appended left to right?
The functions are called, in sequence, left to right. Each will have the usual arguments passed to them.
> In this case the callback have an extra argument next. Does the Javascript compiler always provide next() and it's simply dropped in the above handlers where they have no third argument?
The arguments are passed by a function that is part of Express, not the compiler per se.
Any function that doesn't register a parameter for an argument will drop it (mostly; see the arguments object):
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function foo(a, b, c) {
console.log(a + b);
}
foo(1, 2, 4, 8);
<!-- end snippet -->
答案2
得分: 0
你混淆的是Express的路由和中间件。
中间件函数可以访问请求和响应对象,并传入第三个值/函数next()
,当调用时,允许链进展到下一个中间件函数或进入路由的主要功能。
中间件应用于执行逻辑/额外措施,以预先验证或更改正在进行的请求。中间件的常见用途包括:
- 验证访问令牌
- 验证路由访问
- 解析请求体
- 配置CORS规则
- 等等
你可以在路由中使用任意数量的中间件函数,这就是为什么在你提供的示例2中,a和b都是中间件函数。
中间件函数通过在全局级别使用app.use()
或在路由级别使用route.use()
来附加到路由器上。
英文:
What you're confusing is an Express route and middleware.
Middleware functions have access to the request and response objects and also passes in a third value / function, next()
, which, when called, allows the chain to progress either to the next middleware function or into the main functionality of your route.
Middleware should be used to perform logic / additional measures to prequalify or change a request being made. Common use cases for middleware as things like:
- Validating access tokens
- Validating route access
- Parsing request body
- Configuring CORS rules
- Etc.
You can have any number of middleware functions being used by your route, which is why in example 2 that you've provided, both a & b are middleware functions.
Middleware functions are attached to the router by using app.use()
at a global level or route.use()
at route level.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论