我最近开始使用 node.js、express 和 mongodb。由于 express 使用 connect 来提供中间件支持,我开始阅读中间件和连接。
我在howtonode.org上遇到了以下示例:
return function logItHandle(req, res, next) {
var writeHead = res.writeHead; // Store the original function
counter++;
// Log the incoming request
console.log("Request " + counter + " " + req.method + " " + req.url);
// Wrap writeHead to hook into the exit path through the layers.
res.writeHead = function (code, headers) {
res.writeHead = writeHead; // Put the original back
// Log the outgoing response
console.log("Response " + counter + " " + code + " " + JSON.stringify(headers));
res.writeHead(code, headers); // Call the original
};
// Pass through to the next layer
next();
};
有人可以向我解释这次关闭中发生了什么吗?作者称之为
包装成语以挂钩到对 writeHead 的调用
这意味着什么?