Skip to content
v1.0.0-beta.8

Response

Harpia Core provides a custom Response. It gives you a fluent and expressive API to handle HTTP responses with simplicity and power.

To use it:

import { type Response } from "harpiats";

Features

The custom Response offers intuitive methods for setting status codes, sending JSON, HTML, redirecting, setting cookies, and even rendering views through template engines.


Headers

You can access and manipulate response headers directly using the .headers property:

res.headers.set("X-Custom-Header", "Hello");

More informations about Headers, see the MDN documentation.


res.status(code: number)

Sets the HTTP status code for the response.

res.status(404).send("Not found");

res.send(data: any)

Sends raw data (string, buffer, etc.). Automatically sets the Content-Length header.

res.send("Hello, world!");

res.json(data: any)

Sends a JSON response with the correct Content-Type header.

res.json({ success: true });

res.html(htmlString: string)

Sends raw HTML with the appropriate Content-Type.

res.html("<h1>Hello!</h1>");

res.redirect(url: string, statusCode = 302)

Redirects the request to a given URL with optional status code (default is 302).

res.redirect("/login");
res.redirect("/", 301); // permanent redirect

Cookies

Set cookies easily with:

res.cookies.set("token", "abc123", {
httpOnly: true,
maxAge: 60 * 60 * 24, // 1 day
});

See the Cookies section for full details on cookie options.


Template Rendering

If a template engine is configured, you can render views:

res.render("dashboard", { user });

You can also specify a module name for modular view resolution:

res.module("admin").render("users/list");

Note: res.module() only works if you are using Harpia’s built-in template engine. Make sure to configure the template engine using app.engine(). For more information, see the Template Engine section.


Full Example

router.get("/profile", async (req: Request, res: Response) => {
const user = await getUser(req);
if (!user) {
return res.status(401).json({ error: "Unauthorized" });
}
res.status(200).render("profile", { user });
});