@ashkiani/express-responses v1.0.0
express-responses
Reusable response helpers for Express APIs with integrated logging support — standardize your JSON/text replies and HTTP error handling with one module.
@ashkiani/express-responses
Reusable HTTP response helpers for Express APIs — with optional structured logging.
This utility standardizes how your Express app sends JSON, text, and error responses. It also supports logging response details (e.g., status codes and payloads) when a log object is available.
📦 Installation
npm install @ashkiani/express-responses🛠 Usage
- Import the module:
const resUtils = require('@ashkiani/express-responses');- Use it in your route handlers:
app.get("/api/example", async (req, res) => {
const logEntry = {
requestId: "abc123",
timestamp: new Date()
};
try {
await resUtils.sendJsonResponse(res, 200, { message: "Success!" }, logEntry);
} catch (err) {
await resUtils.send500InternalError(res, logEntry);
}
// Optional: Save logEntry to DB
});📘 API Reference
sendTextResponse(res, statusCode, message, logEntry?)
Sends plain text and, if a logEntry object is provided, logs the response.
sendJsonResponse(res, statusCode, jsonObj, logEntry?)
Sends JSON and logs it if a logEntry object is present.
send204NoContent(res, logEntry?)
Sends a 204 No Content response.
Predefined Error Responses
These are convenience wrappers for common error statuses:
| Function | Description |
|---|---|
send400BadReq() | Bad Request (400) |
send401UnAuth() | Unauthorized (401) |
send500InternalError() | Internal Server Error (500) |
send503ServiceUnavailable() | Service Unavailable (503) |
Each function accepts (res, logEntry?) and safely logs the response if logEntry is provided.
💡 Notes
- Logging is optional. If
logEntryis omitted, the function still works without throwing errors. - These helpers work well with any logging system — just ensure your
logEntryobject includes aresponsefield if you want to store response details. - Compatible with @ashkiani/mongo-logger for full request-response auditing.
📄 License
MIT © Siavash Ashkiani
6 months ago