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 🙏

© 2026 – Pkg Stats / Ryan Hefner

jsend-middleware

v1.0.3

Published

Express middleware for standardized JSend JSON responses

Downloads

17

Readme

JSend Middleware for Express

Node.js Version License

JSend Middleware is a lightweight utility for Express.js applications that implements the JSend specification to standardize JSON API responses. It provides a simple, consistent way to send success, fail, and error responses with customizable status labels and robust input validation.

Features

  • Standardized Responses: Adheres to the JSend specification with success, fail, and error response types.
  • Customizable Status Labels: Allows custom status labels for flexible API designs.
  • Input Validation: Ensures data is JSON-serializable and error messages are non-empty strings.
  • Type Safety: Includes JSDoc annotations for TypeScript compatibility and better IDE support.
  • Express Integration: Seamlessly integrates as middleware, attaching a Jsend instance to the Express response object.
  • Error Handling: Validates the Express response object and input parameters to prevent runtime errors.

Installation

Install the package via npm:

npm install jsend-middleware

Ensure you have Express.js installed in your project:

npm install express

Usage

Setup

Add the jsendMiddleware to your Express application:

import express from "express";
import jsendMiddleware from "jsend-middleware";

const app = express();

// Apply JSend middleware
app.use(jsendMiddleware());

app.get("/example", (req, res) => {
  res.jsend.success({ message: "Hello, World!" });
});

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

Response Types

The middleware attaches a jsend object to the Express res object, providing three methods:

1. Success Response

Used for successful requests.

res.jsend.success({ user: { id: 1, name: "John Doe" } });
// Response:
// {
//   "status": "success",
//   "data": { "user": { "id": 1, "name": "John Doe" } }
// }

With custom status label and HTTP code:

res.jsend.success({ user: { id: 1, name: "John Doe" } }, 201, "ok");
// Response:
// {
//   "status": "ok",
//   "data": { "user": { "id": 1, "name": "John Doe" } }
// }

2. Fail Response

Used for client-side errors (e.g., invalid input).

res.jsend.fail({ error: "Invalid email format" }, 400);
// Response:
// {
//   "status": "fail",
//   "data": { "error": "Invalid email format" }
// }

3. Error Response

Used for server-side or unexpected errors.

res.jsend.error("Database connection failed", 500, {
  code: "DB_ERROR",
  details: { retryAfter: 60 },
  extra: { traceId: "abc123" },
});
// Response:
// {
//   "status": "error",
//   "message": "Database connection failed",
//   "code": "DB_ERROR",
//   "details": { "retryAfter": 60 },
//   "extra": { "traceId": "abc123" }
// }

Configuration

The middleware supports configuration for default status labels:

app.use(
  jsendMiddleware({
    successLabel: "ok",
    failLabel: "invalid",
    errorLabel: "exception",
  })
);

// Example success response with configured label
res.jsend.success({ message: "Configured!" });
// Response:
// {
//   "status": "ok",
//   "data": { "message": "Configured!" }
// }

API Reference

Jsend Class

Constructor

new Jsend(res);
  • res: Express response object (required).
  • Throws an error if res is not a valid Express response object.

Methods

  • success(data, status, statusLabel)

    • data: Optional object or null (default: null).
    • status: HTTP status code (default: 200).
    • statusLabel: Custom status label (default: "success").
    • Throws an error if data is not JSON-serializable.
  • fail(data, status, statusLabel)

    • data: Optional object or null (default: null).
    • status: HTTP status code (default: 400).
    • statusLabel: Custom status label (default: "fail").
    • Throws an error if data is not JSON-serializable.
  • error(message, status, options, statusLabel)

    • message: Error message (default: "Internal Server Error").
    • status: HTTP status code (default: 500).
    • options: Optional object with code, details, and extra fields.
    • statusLabel: Custom status label (default: "error").
    • Throws an error if message is not a non-empty string or options.extra is not an object.

jsendMiddleware

jsendMiddleware(config);
  • config: Optional configuration object.
    • successLabel: Default label for success responses (default: "success").
    • failLabel: Default label for fail responses (default: "fail").
    • errorLabel: Default label for error responses (default: "error").

Error Handling

The middleware includes robust validation:

  • Ensures the Express res object has required methods (status, json).
  • Validates that data in success and fail is an object or null.
  • Ensures message in error is a non-empty string.
  • Validates that options.extra in error is an object.

Requirements

  • Node.js >= 14.0.0
  • Express.js >= 4.0.0

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please submit a pull request or open an issue on the GitHub repository.

Acknowledgments

Inspired by the JSend specification for standardized JSON responses.