'expressjs: which options will hang the request when response methods are not called

Which of these below referred codes (options) will hang the user request when response methods are not called from a route handler.

Option 1: app.get('/user', function(req, res, next) { res.redirect('/user/all'); })

Option 2: app.get('/user', function(req, res, next) {})

Option 3: app.get('/user', function(req, res, next) { console.log('handling req'); })

Option 4: app.get('/user', function(req, res, next) { res.send('user data'); })

Option 5: app.get('/user', function(req, res, next) { res.end(); })



Solution 1:[1]

Its of course, 2 and 3 since the RES object is not processed and the server will keep expecting the terminal methods of a RES object.

Remember, passing a simple return to your method does not ensure res is closed. You have to explicitly process the RES object for your endpoint to correctly end its lifecycle.

Its very bad practice to keep request hanging. In production, this leads to unnecessary complexity because the gateway will respond as 504 Timed out.

Its best to have a global error handler that ensures that all cases of runtime errors go into a catch block where it explicitly throws a 500 request.

Another best practice is to always have a return function at the last line of your request method ( usually controllers ). This will ensure that the request at least closes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sahil Ali