https://github.com/hwz/chirp/blob/master/module-5/completed/routes/api.jsリターン
function isAuthenticated (req, res, next) {
// if user is authenticated in the session, call the next() to call the next request handler
// Passport adds this method to request object. A middleware is allowed to add properties to
// request and response objects
//allow all get request methods
if(req.method === "GET"){
return next();
}
if (req.isAuthenticated()){
return next();
}
// if the user is not authenticated then redirect him to the login page
return res.redirect('/#login');
};
なぜ著者はreturn next()
代わりのnext()
をするのでしょうか?私はnext()
は、次のミドルウェアや機能にジャンプすることができますが、それはなぜ上記のnext()
のために必要ですか?
だから 'isAuthenticated'は' next() 'が返すものを返します。これは有用な価値ではないかもしれません。 –
可能な複製http://stackoverflow.com/questions/16810449/when-to-use-next-and-return-next-in-node-js有効であり、良い質問 –
エクスプレスミドルウェアの文脈では特定のミドルウェアを実行することを停止するかどうかは、それらのifステートメントのいずれかに当てはまります。 'next'を呼び出すと明示的に「ここでやりました」と言われますが、' next'と呼ぶことができる 'return'がなくてもリダイレクトを試みます。 – ste2425