慕慕森
我个人觉得你可以直接看官方的样例里面的几个例子嘛, 比如 cookie-parser 就是一个典型的也符合你面试官口中的中间件.function cookieParser(secret, options) { return function cookieParser(req, res, next) { if (req.cookies) { return next(); } var cookies = req.headers.cookie; var secrets = !secret || Array.isArray(secret) ? (secret || []) : [secret]; req.secret = secrets[0]; req.cookies = Object.create(null); req.signedCookies = Object.create(null); // no cookies if (!cookies) { return next(); } req.cookies = cookie.parse(cookies, options); // parse signed cookies if (secrets.length !== 0) { req.signedCookies = signedCookies(req.cookies, secrets); req.signedCookies = JSONCookies(req.signedCookies); } // parse JSON cookies req.cookies = JSONCookies(req.cookies); next(); };}不过我个人的理解更倾向于, 只要函数的实现方式是类似于function (req, res, next) {
//todo something
}这种方式的函数, 都可以被称为中间件