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.
While STE can effectively render HTML templates, it is a Simple Template Engine suitable for straightforward HTML architectures where the transferred data doesn't need complex manipulation. Therefore, LiteNode provides an html
method extending the response object, enabling users to employ another template engine with LiteNode, such as Eta. I highly recommend Eta for advanced data processing in HTML templates.
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("/htmlpage", (req, res) => {
res.html("<h1>Welcome to the HTML page</h1>")
})
// Route to handle sending HTML response using Eta
app.get("/template", (req, res) => {
const data = {
/* Date needing advanced template processing */
}
const renderHTML = eta.render("my-template.html", data)
res.html(renderHTML)
})
In this example, the /htmlpage
route will send the HTML response <h1>Welcome to the HTML page</h1>
to the client.