API response message for any Node JS application
Install the package in your project
npm quick-response
The package provides a utility function called apiResponse
that allows you to create API response objects
The apiResponse
function accepts the following parameters:
statusCode (number):
- The HTTP status code to be returned with the response.
- Example values: 200 (OK), 404 (Not Found), 500 (Internal Server Error).
message (string):
- A brief message describing the response.
- Example values: 'Success', 'Resource not found', 'Internal server error'.
data (object, optional):
- An optional object containing additional data to be included in the response.
- If not provided, the data field in the response will be set to null.
- Example value: { id: 1, name: 'John Doe' }.
- If you are using common js
const apiResponse = require("quick-response");
console.log(apiResponse(200, "Success", { id: 1 }));
- If you are using module js
import apiResponse from "quick-response";
console.log(apiResponse(200, "Success", { id: 1 }));
Response
{
statusCode: 200,
message: 'Success',
data: { id: 1 },
timestamp: 'Fri Aug 30 2024'
}
import express from "express";
import apiResponse from "quick-response";
const app = express();
const port = 8000;
app.get("/", (req, res) => {
res.json(apiResponse(201, "User Created", { id: 1, name: "John Doe" }));
});
app.listen(port, () => console.log("Server is running"));
Result
{
"statusCode": 201,
"message": "User Created",
"data": {
"id": 1,
"name": "John Doe"
},
"timestamp": "Fri Aug 30 2024"
}
Happy Coding...👍
This project is licensed under the MIT License - see the LICENSE file for details.