在浏览快递一些老的文档,我发现这个例子中,使用回调“关闭”事件:
app.get('/page/', function (req, res) {
res.writeHead(200, { /* . . . */ );
res.write('\n');
req.on("close", function () {
// do something when the connection is closed
});
});
在这ES10认为好的做法呢?我应该写这样的代码,或者我需要考虑事件的其他方法?
这仍然是标准的做法。正如你所看到的这个例子从4.x (latest) Express.js docs取
app.get('/', function (req, res) {
res.send('GET request to homepage');
});
有一件事,如果你愿意,你可以做的,就是用arrow function notation来定义您的回调。像这样
app.get('/page/', (req, res) => {
res.writeHead(200, { /* . . . */ });
res.write('\n');
req.on("close", () => {
// do something when the connection is closed
});
});