https://github.com/hwz/chirp/blob/master/module-5/completed/routes/api.jspowrócić next() w nodejs zamieszania
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');
};
Dlaczego autor zrobić return next()
zamiast next()
? Wiem, że next()
to pozwolić przepływowi przejść do następnego oprogramowania pośredniego lub funkcji, ale dlaczego potrzebuje on return
dla next()
powyżej?
Tak więc 'jestAuthenticated' zwraca wszystko, co' next() 'zwraca. To może nie być przydatna wartość, zwróćcie uwagę. –
możliwy duplikat http://stackoverflow.com/questions/16810449/when-to-use-next-and-return-next-in-node-js, ale jest prawidłowe i dobre pytanie –
W kontekście ekspresowych artykułów średnich jej zatrzymanie wykonywania tego konkretnego elementu middle-ware powinno się do jednej z tych instrukcji if. Wywołanie 'next' mówi express 'im done here move on', jednak bez' return' może wywołać 'next', ale spróbuj także wykonać przekierowanie. – ste2425