I have the following minimal basic express node js application:
var express = require ('express');
var app = express ();
app.get ('/', function (req, res) {
res.send ('Hello');
});
app.listen (3000);
When I access this site on localhost:3000 I get a response that looks like:
If I change res.send ('Hello');
to res.end ('Hello');
, the response is a different font like:
I am curious; why the difference?
res. send() is used to send the response to the client where res. end() is used to end the response you are sending.
res. json forces the argument to JSON. res. send will take an non-json object or non-json array and send another type.
There is no actual difference between res. send and res.
end() Function. The res. end() function is used to end the response process. This method actually comes from the Node core, specifically the response.
If you pass a string to res.send()
, it automatically assumes a Content-Type of html.
res.end()
, however, simply calls node's underlying end()
implementation on the response stream, so no assumptions are made for the Content-Type.
The reason it renders differently is simply a browser decision to render a "pretty" default font for HTML, and a less-styled font for unknown content types.
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