- A simple and easy-to-use utility for returning standard HTTP responses in a Node.js application, designed for use in a MERN stack backend.
npm nodejs-return-status
- This package provides functions to return standard success and error responses in your Express API.
You can import the functions in your project like this:
const { ReturnSuccessMsg, ReturnResourceExists } = require('nodejs-return-status');
- Returns only success response with HTTP status code
200
Parameters
-
res
: The response object from Express.
- Returns a success response with a custom message and a
200
OK status code.
Parameters
-
res
: The response object from Express. -
msg
: Message that you need to send
- Returns an error response with a
409
Conflict status code, indicating that the resource already exists.
Parameters
-
res
: The response object from Express. -
Error
: Error Message that you need to send
- Returns an error response with a
500
Internal Server Error status code.
Parameters
-
res
: The response object from Express. -
Error
: Error Message that you need to send
const express = require('express');
const { ReturnSuccessMsg, ReturnResourceExists } = require('nodejs-return-status');
const app = express();
// Example Route to register a user
app.post('/register', (req, res) => {
const { email } = req.body;
// Simulate checking if user already exists
const userExists = email === 'existinguser@example.com';
if (userExists) {
return ReturnResourceExists(res, "User Already exists"); // Resource already exists
}
return ReturnSuccessMsg(res, "User Registered");
});
app.listen(3000, () => console.log('Server running on port 3000'));
-
Status Codes and Responses
-
200 OK
: Returned by ReturnSuccessMsg() to indicate successful operation. -
409 Conflict
: Returned by ReturnResourceExists() to indicate a conflict (e.g., when a resource already exists).
This package is licensed under the MIT License
- Fork the repository.
- Create a new branch
- Commit your changes
- Push to the branch
- Create a new Pull Request.
- Author: JehanKandy
- GitHub: BackendExpert
- NPM: jehankandy