useResponseHelpers()
Signature
Section titled “Signature”app.useResponseHelpers()Description
Section titled “Description”Installs an application-level middleware that extends the Express Response object (res) with semantic, chainable helper methods for standard HTTP status codes.
This method returns the KaelumApp instance, allowing it to be chained with other configuration methods.
Examples
Section titled “Examples”// Enable globallyapp.useResponseHelpers();
app.post('/api/data', (req, res) => { try { const data = processData(req.body); // Returns 201 Created with JSON body res.created(data); } catch (err) { // Returns 400 Bad Request with { "error": "Invalid format" } res.badRequest("Invalid format"); }});Response Formatting Behavior
Section titled “Response Formatting Behavior”All helpers (except noContent) accept a payload parameter and dynamically determine how to respond:
- String payload: Sent as
text/html(or mapped to{ "error": "string" }for error methods). - Object/Array payload: Sent as
application/json. - Undefined payload: Ends the response without a body.
Provided Methods
Section titled “Provided Methods”Success Helpers
Section titled “Success Helpers”res.ok([data]): Sets status to200 OK.res.created([data]): Sets status to201 Created.res.noContent(): Sets status to204 No Contentand ends the response.
Error Helpers
Section titled “Error Helpers”If passed a string, error helpers automatically format the response as { "error": "Your string here" }.
res.badRequest([err]): Sets status to400 Bad Request.res.unauthorized([err]): Sets status to401 Unauthorized.res.forbidden([err]): Sets status to403 Forbidden.res.notFound([err]): Sets status to404 Not Found.res.conflict([err]): Sets status to409 Conflict.res.error([err], [statusCode=500]): Sets status to the providedstatusCode(defaults to500 Internal Server Error).