Response Helpers API
Complete reference for every response helper in MoroJS. Three layers of helpers give you full control — from one-liner convenience methods to fluent builders for complex scenarios.
On this page
Overview
MoroJS provides three layers of response helpers. Pick the one that fits your needs — they all produce the same consistent response shape.
| Layer | Sets HTTP Status? | Sends Response? | Import |
|---|---|---|---|
| res.* | Yes | Yes | Available on every res object |
| response.* | No | No | import { response } from '@morojs/moro' |
| ResponseBuilder | No | No | import { ResponseBuilder } from '@morojs/moro' |
Types
All handler signatures use HttpRequest and HttpResponse from @morojs/moro.
Import
typescript
HttpRequest
Extends Node's IncomingMessage with parsed properties:
HttpRequest Interface
typescript
HttpResponse
Extends Node's ServerResponse with all the methods documented below:
HttpResponse Type
typescript
Handler Signatures
Handler & Middleware Types
typescript
CookieOptions
CookieOptions Interface
typescript
ResponseState
ResponseState Interface
typescript
Core Methods
Foundational methods on every res object for sending data, setting status codes, managing cookies, and more.
res.json(data)
Serializes data as JSON and sends it. Sets Content-Type: application/json.
Usage
typescript
res.status(code)
Sets the HTTP status code. Returns res for chaining.
Usage
typescript
res.send(data)
Sends a string or Buffer response.
Usage
typescript
res.cookie(name, value, options?)
Sets a cookie on the response. Returns res for chaining. See CookieOptions above for available options.
Usage
typescript
res.clearCookie(name, options?)
Clears a cookie. Returns res for chaining.
Usage
typescript
res.redirect(url, status?)
Redirects to the given URL. Defaults to 302.
Usage
typescript
res.sendFile(filePath)
Streams a file as the response. Returns a Promise<void>.
Usage
typescript
res.render(template, data?)
Renders a template (if a template engine is configured). Returns a Promise<void>.
Usage
typescript
Success & Error Responses
These methods set the HTTP status code and send the response in a single call. After calling any of these, the response is finished.
Success Responses
res.success(data, message?)
200 OK
Usage
typescript
Response Body
json
res.created(data, location?)
201 Created
Optionally sets the Location header.
Usage
typescript
Response Body
json
res.noContent()
204 No Content
Sends an empty body. Perfect for delete operations.
Usage
typescript
res.paginated(data, pagination)
200 OK
Pagination metadata (totalPages, hasNext, hasPrev) is calculated automatically.
| Parameter | Type |
|---|---|
| data | T[] |
| pagination | { page: number; limit: number; total: number } |
Usage
typescript
Response Body
json
Error Responses
res.error(error, code?, message?)
200Sends a 200 OK response (does not set a non-200 status code) with an error body. Use res.status() beforehand if you need a specific HTTP code, or use one of the specific error helpers below.
Usage
typescript
res.badRequest(message?)
400'Invalid request'Usage
typescript
Response Body
json
res.unauthorized(message?)
401'Authentication required'Usage
typescript
Response Body
json
res.forbidden(message?)
403'Insufficient permissions'Usage
typescript
Response Body
json
res.notFound(resource?)
404'Resource'Usage
typescript
Response Body
json
res.conflict(message)
409requiredUsage
typescript
Response Body
json
res.validationError(errors)
422Sends field-level error details. Each error has field, message, and optional code.
Usage
typescript
Response Body
json
res.rateLimited(retryAfter?)
429When retryAfter (seconds) is provided, the Retry-After header is set automatically.
Usage
typescript
Response Body
json
res.internalError(message?)
500'Internal server error'Usage
typescript
Response Body
json
Header & State Utilities
Low-level utilities for inspecting and manipulating headers and response state.
res.hasHeader(name)
Returns true if the given header has been set.
Usage
typescript
res.setBulkHeaders(headers)
Sets multiple headers at once. Returns res for chaining.
Usage
typescript
res.appendHeader(name, value)
Appends a value to an existing header (or creates it). Returns res for chaining.
Usage
typescript
res.canSetHeaders()
Returns true if headers have not been sent yet and can still be modified.
Usage
typescript
res.getResponseState()
Returns the current ResponseState object.
Usage
typescript
Standalone Helper Functions
These functions build response body objects only. They do not set HTTP status codes or send the response. Use them with res.status().json() for full control, or return them from a handler (which sends 200 by default).
Import
typescript
Quick Reference
| Function | Returns |
|---|---|
| response.success(data, message?) | { success: true, data, message? } |
| response.error(error, code?, message?) | { success: false, error, code?, message? } |
| response.validationError(details, message?) | { success: false, error: "Validation failed", ... } |
| response.unauthorized(message?) | { success: false, error: "Unauthorized", ... } |
| response.forbidden(message?) | { success: false, error: "Forbidden", ... } |
| response.notFound(resource?) | { success: false, error: "Not Found", ... } |
| response.conflict(message) | { success: false, error: "Conflict", ... } |
| response.badRequest(message?) | { success: false, error: "Bad Request", ... } |
| response.internalError(message?) | { success: false, error: "Internal Server Error", ... } |
| response.rateLimited(retryAfter?) | { success: false, error: "Too Many Requests", ... } |
Return directly (sends 200 with body)
typescript
Pair with res.status() for proper HTTP codes
typescript
ResponseBuilder
A fluent API for building complex responses. Returns body objects only (no HTTP status or send). Use method chaining to compose responses step by step.
Import
typescript
Builder Methods
| Method | Description |
|---|---|
| ResponseBuilder.success(data) | Start a success response |
| ResponseBuilder.error(error, code?) | Start an error response |
| .message(msg) | Add a message |
| .details(details) | Add details (error responses) |
| .code(code) | Add an error code |
| .build() | Return the final response object |
Success with ResponseBuilder
typescript
Error with ResponseBuilder
typescript
Important: Return Values vs HTTP Status Codes
When a handler returns a plain object, the framework serializes it as JSON with a 200 OK status. A status field in the JSON body does not affect the HTTP status code.
The Pitfall — This sends HTTP 200, NOT 403
typescript
To send an actual HTTP 403, use one of these:
Option 1: res.* helper (Recommended)
Option 2: res.status().json() with standalone helper
Option 3: res.status().json() manually
Rule of thumb: If you need an HTTP status other than 200, always use a res.* method or res.status() explicitly.
Quick Reference — All res.* Methods
Core
res.json(data)res.status(code)res.send(data)res.cookie(name, val, opts?)res.clearCookie(name, opts?)res.redirect(url, status?)res.sendFile(path)res.render(tpl, data?)
Success
res.success(data, msg?)— 200res.created(data, loc?)— 201res.noContent()— 204res.paginated(data, pg)— 200
Utilities
res.hasHeader(name)res.setBulkHeaders(hdrs)res.appendHeader(name, val)res.canSetHeaders()res.getResponseState()
Errors
res.error(err, code?, msg?)— 200res.badRequest(msg?)— 400res.unauthorized(msg?)— 401res.forbidden(msg?)— 403res.notFound(resource?)— 404res.conflict(msg)— 409res.validationError(errs)— 422res.rateLimited(retry?)— 429res.internalError(msg?)— 500