Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is necessary to call end after json in node.js?

In express and node a return a json and after i call end.

Is necessary to call

res.end()

after

res.json()

?

like image 234
Pasalino Avatar asked Jun 01 '17 17:06

Pasalino


People also ask

Does Res JSON end the function?

send() (and remember, res. json() ) both allow us to send some data and they also end the response, so there's no need to explicitly call res.

Do you need to call next () in Express?

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging. An Express application can use the following types of middleware: Application-level middleware.

What res end () does?

end() Function. The res. end() function is used to end the response process. This method actually comes from the Node core, specifically the response.

Can I call next after Res send?

res. send() or res. json() should end all writing to the response stream and send the response. However, you absolutely can call next() if you want to do further processing after the response is sent, just make sure you don't write to the response stream after you call res.


3 Answers

You don't have to call res.end() if you call res.json(). res.json() calls res.end() for you.

like image 173
Alfran Avatar answered Nov 11 '22 19:11

Alfran


Example from docs; https://expressjs.com/en/api.html

You do not need.

enter image description here

like image 39
Teoman shipahi Avatar answered Nov 11 '22 20:11

Teoman shipahi


No, it's not necessary to call res.end() after you have called res.json() (or res.send() or res.render()).

like image 32
robertklep Avatar answered Nov 11 '22 21:11

robertklep