GitHub GitHub
docs / Response Methods

Response Methods

LiteNode extends the native Node.js response object with convenience methods for sending typed content with appropriate headers.

Overview

Method Content-Type Use case
sendFile() auto-detected Send a file from disk
txt() text/plain Plain text
html() text/html HTML string
xml() application/xml XML / RSS / sitemap
json() application/json JSON data
redirect() Redirect to another URL
status() Set HTTP status code (chainable)

sendFile

sendFile(filePath: string): void

Reads a file from disk, sets the correct Content-Type based on the extension, and sends it as the response body. Displays in the browser for supported types — does not force a download.

app.get("/resume", (req, res) => {
    res.sendFile("./files/resume.pdf")
})

Supported Extensions

Browser-renderable: .html, .css, .js, .mjs, .avif, .gif, .ico, .jpeg, .jpg, .png, .svg, .webp, .txt, .json, .xml, .pdf, .wav, .mp3, .mp4, .webm, .ogg

Download: .zip


txt

txt(text: string): void
app.get("/ping", (req, res) => {
    res.txt("pong")
})

html

html(html: string, statusCode?: number): void
app.get("/welcome", (req, res) => {
    res.html("<h1>Welcome</h1>")
})

xml

xml(xmlContent: string, statusCode?: number): void

Useful for RSS feeds, sitemaps, and XML APIs.

app.get("/feed", (req, res) => {
    res.xml(`<rss version="2.0"><channel><title>My Feed</title></channel></rss>`)
})

json

json(data: any): void

Automatically stringifies the value and sets Content-Type: application/json.

app.get("/api/users", (req, res) => {
    res.json([
        { id: 1, name: "Alice" },
        { id: 2, name: "Bob" },
    ])
})

redirect

redirect(location: string, statusCode?: number): void

Default status code is 302 (temporary). Use 301 for permanent redirects.

app.get("/old-page", (req, res) => {
    res.redirect("/new-page", 301)
})

status

status(code: number): this

Sets the HTTP status code and returns the response for chaining.

app.get("/not-found", (req, res) => {
    res.status(404).txt("Not found")
})

Combining with other methods

All response methods can be combined with status():

res.status(201).json({ created: true })
res.status(500).html("<h1>Internal Server Error</h1>")
res.status(404).json({ error: "Resource not found" })

Note that res.writeHead() + res.end() is equivalent to the chained methods:

// These are identical
res.writeHead(201, { "Content-Type": "application/json" })
res.end(JSON.stringify({ message: "Created" }))

res.status(201).json({ message: "Created" })

See the Examples page for real-world usage patterns.