I am a beginner to express.js
and I am trying to understand the difference between res.send
and res.write
?
res. send() is used to send the response to the client where res. end() is used to end the response you are sending.
There is no actual difference between res. send and res. json, both methods are almost identical.
send() function basically sends the HTTP response. The body parameter can be a String or a Buffer object or an object or an Array. Syntax: res.send( [body] ) Parameter: This function accepts a single parameter body that describe the body which is to be sent in the response.
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.
res.send
res.send
is only in Express.js.Content-Length
HTTP response header field.res.send
can only be called once, since it is equivalent to res.write
+ res.end()
app.get('/user/:id', function (req, res) { res.send('OK'); });
For more details:
res.write
response.write('<html>'); response.write('<body>'); response.write('<h1>Hello, World!</h1>'); response.write('</body>'); response.write('</html>'); response.end();
For more details:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With