Skip to content

Responses

Gaman Response here we use the Res function, you don’t need to import it first because the Res function here is already a global function so you can use it anywhere.

Import the Response class and use it in your route handler:

(ctx) => {
return Res.json({ message: "Hello World" }, { status: 200 });
};

FYI: Response status can be shorthand like this

(ctx) => {
return Res.json({ message: "Bad Request" }, 403); // directly enter status number
};

Returns a JSON response with Content-Type: application/json.

(ctx) => {
return Res.json({
user: { id: 1, name: "Angga" },
status: "success",
});
};

// With custom options

(ctx) => {
return Res.json(
{ error: "Not found" },
{ status: 404, headers: { "X-Custom": "value" } }
);
};

Returns a plain text response with Content-Type: text/plain.

(ctx) => {
return Res.text("Hello, World!");
};

// With custom status

(ctx) => {
return Res.text("Server Error", { status: 500 });
};

Returns an HTML response with Content-Type: text/html.

(ctx) => {
return Res.html(`
<html>
<body>
<h1>Welcome</h1>
<p>Hello from GamanJS!</p>
</body>
</html>
`);
};

// With custom header

(ctx) => {
return Res.html("<h1>Custom Page</h1>", {
status: 201,
headers: { "X-Frame-Options": "DENY" },
});
};

Renders a template using the configured view engine. Requires view engine integration setup first.

(ctx) => {
return Res.render("home", {
title: "Welcome",
user: { name: "Angga" },
});
};

// With custom status

(ctx) => {
return Res.render("error", { message: "Page not found" }, { status: 404 });
};

Returns a streaming response with Content-Type: application/octet-stream.

import { createReadStream } from "fs";
(ctx) => {
const fileStream = createReadStream("./large-file.pdf");
return Res.stream(fileStream);
};

// With custom content type

(ctx) => {
const videoStream = createReadStream("./video.mp4");
return Res.stream(videoStream, {
headers: { "Content-Type": "video/mp4" },
});
};

Returns a redirect response. Default status is 302 (temporary redirect).

(ctx) => {
return Res.redirect("/login");
};

// Permanent redirect (301)

(ctx) => {
return Res.redirect("/new-url", 301);
};

// Temporary redirect (302) - explicit

(ctx) => {
return Res.redirect("/dashboard", 302);
};

Shortcut method to resolve the request with status code 200.

ctx => {
return Res.ok({ message: "OK" })
}

Shortcut method to resolve the request with status code 201.

ctx => {
return Res.created({ message: "Created" })
}

Shortcut method to resolve the request with status code 202.

ctx => {
return Res.accepted({ message: "Accepted" })
}

Shortcut method to resolve the request with status code 203.

ctx => {
return Res.nonAuthoritativeInformation({ message: "Non Authoritative Information" })
}

Shortcut method to resolve the request with status code 204 without content.

ctx => {
return Res.noContent()
}

Shortcut method to resolve the request with status code 205 without content.

ctx => {
return Res.resetContent()
}

Shortcut method to resolve the request with status code 206.

ctx => {
return Res.partialContent({ message: "Partial Content" })
}

Shortcut method to resolve the request with status code 300.

ctx => {
return Res.multipleChoices({ message: "Multiple Choices" })
}

Shortcut method to resolve the request with status code 301.

ctx => {
return Res.movedPermanently({ message: "Moved Permanently" })
}

Shortcut method to resolve the request with status code 302.

ctx => {
return Res.movedTemporarily({ message: "Moved Temporarily" })
}

Shortcut method to resolve the request with status code 303.

ctx => {
return Res.seeOther({ message: "See Other" })
}

Shortcut method to resolve the request with status code 304.

ctx => {
return Res.notModified({ message: "Not Modified" })
}

Shortcut method to resolve the request with status code 305.

ctx => {
return Res.useProxy({ message: "Use Proxy" })
}

Shortcut method to resolve the request with status code 307.

ctx => {
return Res.temporaryRedirect({ message: "Temporary Redirect" })
}

(can be continued for all 4xx and 5xx codes with the same format)

The GamanJS Response system provides convenience and flexibility, allowing you to choose between explicit response methods and automatic shortcuts according to your needs.