Documentation
Sending HTML
The html
method is an essential addition to the response object, streamlining the process of sending HTML responses. By setting the Content-Type
header to text/html
and delivering the provided HTML content as the response body, this method ensures that HTML content is correctly handled and returned to the client. This feature is particularly useful for API endpoints or responses where HTML content is required.
html
Description:
The html
method sets the Content-Type
header to text/html
and sends the provided HTML content as the response body. This method simplifies the process of returning HTML responses to the client.
Signature:
html(html: string, statusCode?: number): void
Parameters:
html
(string): The HTML content to be sent as the response body.statusCode
(number, optional): The HTTP status code to be sent. Default is 200.
Example:
// Route to handle sending HTML response
app.get("/html-page", (req, res) => {
res.html("<h1>Welcome to the HTML page</h1>")
})
In this example, the /html-page
route will send the HTML response <h1>Welcome to the HTML page</h1>
to the client.