npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

response-handler-express

v1.1.6

Published

Response Handler is a streamlined and customizable HTTP response handler for Node.js applications using Express. It provides a set of standardized methods to handle a full range of HTTP status codes, from successful responses to client and server errors.

Downloads

1,132

Readme

Response Handler Express

Overview

Response Handler Express is a lightweight, easy-to-use HTTP response handler for Express applications. It provides a set of methods to standardize and simplify response handling for various HTTP status codes, from successful responses to client and server errors.

Features

  • Handles various HTTP status codes (e.g., 200, 201, 400, 401, etc.)
  • Simplifies response handling in Express applications
  • Improves code readability and maintainability

Installation

To install the package, run the following command:

npm install response-handler-express

Usage

Here's a basic example of how to use Response Handler in an Express application:

Features

  • Standardized Responses: Pre-built methods for common HTTP status codes, including 200 OK, 400 Bad Request, 401 Unauthorized, 404 Not Found, 500 Internal Server Error, and more.
  • Customizable Responses: Accepts a message, data, and token (optional), allowing flexibility in response structure.
  • Improved Code Readability: Centralizes response handling for cleaner and more maintainable code.
  • Ideal for REST APIs: Perfect for consistent responses in RESTful services.

Usage

JavaScript

const express = require("express");
const ResponseHandler = require("response-handler-express");

const app = express();

app.get("/", (req, res) => {
  ResponseHandler.success(res, "Request successful", { data: "Sample data" });
});

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

TypeScript

import express, { Request, Response } from "express";
import ResponseHandler from "response-handler-express";

const app = express();

app.get("/", (req: Request, res: Response) => {
  ResponseHandler.success(res, "Request successful", { data: "Sample data" });
});

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

Methods

  • success(res, message, data?): Sends a 200 OK response.
  • created(res, message, data?): Sends a 201 Created response.
  • accepted(res, message, data?): Sends a 202 Accepted response.
  • noContent(res): Sends a 204 No Content response.
  • badRequest(res, message, data?): Sends a 400 Bad Request response.
  • unauthorized(res, message, data?): Sends a 401 Unauthorized response.
  • forbidden(res, message, data?): Sends a 403 Forbidden response.
  • notFound(res, message, data?): Sends a 404 Not Found response.
  • conflict(res, message, data?): Sends a 409 Conflict response.
  • gone(res, message, data?): Sends a 410 Gone response.
  • unprocessableEntity(res, message, data?): Sends a 422 Unprocessable Entity response.
  • tooManyRequests(res, message, data?): Sends a 429 Too Many Requests response.
  • internalError(res, message, data?): Sends a 500 Internal Server Error response.
  • badGateway(res, message, data?): Sends a 502 Bad Gateway response.
  • serviceUnavailable(res, message, data?): Sends a 503 Service Unavailable response.
  • gatewayTimeout(res, message, data?): Sends a 504 Gateway Timeout response.
  • logInSuccess(res, message, data?, token?): Sends a 200 OK response for successful login including an optional token.
  • tokenExpired(res, message?): Sends a 401 Unauthorized response when a session token is expired.
  • invalidToken(res, message?): Sends a 401 Unauthorized response for an invalid token.

License

This project is licensed under the ISC License.

Author

Abdelrahman Ashraf Osman