简体   繁体   English

app.get() 有 3 个参数,我需要一个 explnation Node JS,express

[英]app.get() has 3 parameters, I need an explnation Node JS , express

I'm a noob.我是菜鸟。 I have a question.我有个问题。 I'm using passport-google-oauth20我正在使用 passport-google-oauth20

app.get('/auth/google/secrets',
passport.authenticate('google',{failureRedirect: '/login'}),
function(req,res){
  res.redirect('/secrets');
});

as you can clearly see, this rout ( app.get() ) has 3 parameters, and this is the first time I used something like this, can anyone please explain the logic/theory behind this?你可以清楚地看到,这个 rout ( app.get() ) 有 3 个参数,这是我第一次使用这样的东西,谁能解释一下这背后的逻辑/理论?

normally I use通常我用

app.get('/somepage' , function(req,res,next){//something});

but in this special case, there are 3 parameters.但在这种特殊情况下,有 3 个参数。 can you provide me with any documentation for this specific situation?你能为我提供有关这种特定情况的任何文件吗?

The code is perfectly fine, I just need an explnation.代码非常好,我只需要一个解释。

app.get() accepts a minimum of two arguments as shown in the doc , but you can pass as many callbacks as you want (one or more): app.get()接受至少两个 arguments ,如文档中所示,但您可以根据需要传递任意数量的回调(一个或多个):

app.get(path, callback [, callback ...])

Each callback you pass is executed in series one after the other.您传递的每个回调都依次执行。 The second one doesn't get executed until the first one calls next() in its handler and so on for the other ones.在第一个调用其处理程序中的next()之前,第二个不会执行,依此类推。

This allows you to have middleware that is specific to just this route.这允许您拥有特定于此路由的中间件。 In the case you show in your question, it's inserting some middleware to require authentication before the main request handler is executed.如果您在问题中显示,它会插入一些中间件以在执行主请求处理程序之前要求进行身份验证。

If that authentication fails, the middleware will send a response and the final callback will not be called.如果身份验证失败,中间件将发送响应并且不会调用最终回调。 If the authentication passes, it will call next() and the final callback will then be executed.如果身份验证通过,它将调用next()并执行最终回调。

Here's a illustrative example:这是一个说明性示例:

app.get("/test", 
   (req, res, next) => {
     console.log("in first handler");
     next();
}, (req, res, next) => {
     console.log("in second handler");
     next();         
}, (req, res, next) => {
     console.log("in third handler, sending response");
     // sending response and not calling next()
     res.send("ok");
}, (req, res, next) => {
     // won't ever get here
     console.log("in final handler");
     res.send("hi");
});

This shows four request handlers being passed to an app.get() .这显示了四个请求处理程序被传递给app.get() The output from this in the debug console on the server will be:服务器调试控制台中的 output 将是:

in first handler
in second handler
in third handler, sending response

And, the response from this request will be:并且,此请求的响应将是:

ok

The fourth handler will not be called because the third handler does not call next() .第四个处理程序不会被调用,因为第三个处理程序没有调用next() Instead, it just sends a response to the request.相反,它只是发送对请求的响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM