Javascript express.js middleware中的下一个()函数是什么
我很难理解在这个Express.js示例中接下来要做什么,以及为什么在这里使用它 这是我处理路线的文件Javascript express.js middleware中的下一个()函数是什么,javascript,node.js,express,Javascript,Node.js,Express,我很难理解在这个Express.js示例中接下来要做什么,以及为什么在这里使用它 这是我处理路线的文件 const express = require('express'); const controller = require('../controllers/myappcontroller'); const myroutes = express.Router(); const apphelper = require('../services/appservices/apphelper'
const express = require('express');
const controller = require('../controllers/myappcontroller');
const myroutes = express.Router();
const apphelper = require('../services/appservices/apphelper');
myroutes.get('/', apphelper.mycoolfunction, controller.index);
这是apphelper.js文件的内容
require('isomorphic-fetch');
function mycoolfunction(req, res, next) {
fetch('someurl')
.then((fetchRes) => {
return fetchRes.json();
}).then((jsonFetchRes) => {
res.locals.firstname = jsonFetchRes.contents.firstname[0].firstname;
next();
}).catch((err) => {
console.log(err);
res.locals.firstname = 'not available';
next();
});
}
module.exports = {
mycoolfunction: mycoolfunction,
};
为什么参数中有一个“next”以及为什么在函数中使用它们?它的存在是否使得简单,它告诉你的应用程序运行下一个中间件。简单,它告诉你的应用程序运行下一个中间件。它跳到下一个中间件。如果有
.get('/',func1,func2,func3)
,则func1中的next()
将跳转到func2,func2中的next()
将跳转到func3。因此,在本例中,如果没有“next”,它将停止在res.locals.firstname='notavailable'代码>而不做任何其他事情?由于“next”的存在,它运行行中的下一个参数,在本例中,controller.index
correct?是的,这非常好,非常感谢您跳到下一个中间件。如果有.get('/',func1,func2,func3)
,则func1中的next()
将跳转到func2,func2中的next()
将跳转到func3。因此,在本例中,如果没有“next”,它将停止在res.locals.firstname='notavailable'代码>而不做任何其他事情?由于“next”的存在,它运行行中的下一个参数,在本例中,controller.index
correct?是的,非常好,非常感谢这有点轻…这有点轻。。。