Documentation
Sending Text
The txt
method is a convenient addition to the response object that simplifies sending plain text responses. By setting the Content-Type
header to text/plain
and sending the provided text as the response body, this method ensures that plain text content is correctly handled and returned to the client. It is particularly useful for API endpoints or responses where plain text is the desired format.
txt
Description:
The txt
method sets the Content-Type
header to text/plain
and sends the provided plain text as the response body. This method simplifies the process of returning plain text responses to the client.
Signature:
txt(text: string): void
Returns:
text
(string): The plain text content to be sent as the response body.
Example:
// Route to handle sending plain text response
app.get("/plaintext", (req, res) => {
res.txt("This is a plain text response.")
})
In this example, the /plaintext
route will send the plain text response "This is a plain text response." to the client.