Reputation: 121
While browsing some old documentation for express, I found this example that uses callbacks for the "close" event:
app.get('/page/', function (req, res) {
res.writeHead(200, { /* . . . */ );
res.write('\n');
req.on("close", function () {
// do something when the connection is closed
});
});
Is this considered good practice in ES10? Should I write code like this, or do I need to consider other methods for events?
Upvotes: 2
Views: 36
Reputation: 4126
This is still standard practice. As you can see this example is taken from the 4.x (latest) Express.js docs
app.get('/', function (req, res) {
res.send('GET request to homepage');
});
One thing you can do if you want, is to use arrow function notation to define your callbacks. Like this
app.get('/page/', (req, res) => {
res.writeHead(200, { /* . . . */ });
res.write('\n');
req.on("close", () => {
// do something when the connection is closed
});
});
Upvotes: 3