最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

我应该使用回调的事件?

运维笔记admin23浏览0评论

我应该使用回调的事件?

我应该使用回调的事件?

在浏览快递一些老的文档,我发现这个例子中,使用回调“关闭”事件:

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
    });
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论