quick-response
v1.0.5
Published
API response message for Node JS application
Downloads
29
Maintainers
Readme
quick-response
API response message for any Node JS application
Installation
Install the package in your project
npm quick-response
Usage
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'
}
Usage with Express
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...👍
License
This project is licensed under the MIT License - see the LICENSE file for details.