https://github.com/hwz/chirp/blob/master/module-5/completed/routes/api.jsवापसी अगले() NodeJS भ्रम की स्थिति में
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()
फ्लो को अगली मिडलवेयर या फ़ंक्शन पर जाने देना है, लेकिन इसे return
को next()
के ऊपर क्यों चाहिए?
तो 'प्रमाणीकृत' जो कुछ भी अगला() 'रिटर्न देता है। यह एक उपयोगी मूल्य नहीं हो सकता है, आपको दिमाग। –
http://stackoverflow.com/questions/16810449/when-to-use-next-and-return-next-in-node-js के संभावित डुप्लिकेट लेकिन यह मान्य और अच्छा प्रश्न –
एक्सप्रेस मध्य-सामान के संदर्भ में मध्य-बर्तन के उस विशिष्ट टुकड़े को निष्पादित करने के लिए इसे रोकना चाहिए यदि यह बयानों में से किसी एक में गिरना चाहिए। कॉलिंग 'अगली' एक्सप्रेस को बताती है 'मैंने यहां किया है' हालांकि 'रिटर्न' के बिना यह 'अगली' कॉल कर सकता है लेकिन फिर रीडायरेक्ट करने का प्रयास करता है। – ste2425