Mastering responses
Responses are the way by which the server communicates back to the client after a request, so it is crucial to understand how to manage them. In this section, we will learn about adding headers, status codes, redirects, sending data, and sending files.
You will discover the available methods when you start to build more complex applications. You can find more information about the response object in the Express documentation (https://expressjs.com/en/4x/api.html#res).
Header management
Headers are used to send extra information about the response. Express handles headers by using the set
method, which receives two parameters, the name of the header and the value of the header:
app.get('/', (req, res, next) => { res.set('Content-Type', 'text/html') res.send("<h1>Hello World</h1>") })
In the preceding example, we are setting the Content-Type
header...