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

node-response

v1.1.1

Published

A simple API response handler for Node/Express

Downloads

13

Readme

Node Response

A simple API response handler for Node/Express.

{
	"status": 200,
	"success": true,
	"error": null,
	"data": {
		"hello": ["world"]
	},
	"meta": null
}

Example response from API.

Installation

node-response can be installed using NPM or Yarn.

NPM
npm install node-response
Yarn
yarn add node-response

Usage

Examples will show how to use node-response with Express, but as long as the HTTP response object contains these methods:

  • status
  • json
  • end

Then you can just plug and play.

Setting up middleware

Express allows middleware to be added on the application level or the router level, this means that any routes that extend from the highest source using the middleware will inherit the response. Read more about using middleware here.

// router

// Create router instance
const express = require('express');
const router = express.Router();

// Add node response middleware
router.use(require('node-response').middleware);

// Set other routes
router.use(require('./routes/auth'));

// Export to be used in application
module.exports = router;

Adding the node-response middleware on the router level.

Accessing the response

When accessing the response through after setting up the middleware it can be found in the res.locals object as handler.

// ./routes/auth

function authenticate(_, res) {
    res.locals.handler.json();
}

module.exports = {
    authenticate
}

Returning JSON from the response instance created by the middleware.

If you need to access the response directly you can do so two different ways:

const response = require('node-response');

// or
const response = require('node-response').response;

(new response).json();

Creating new response objects and returning JSON.

Returning a JSON HTTP Response

To return a JSON HTTP Response you must pass the Express Response into the API handler, or if you are using the middleware then this is done for you.

// ./routes/auth
const response = require('node-response');

function authenticate(_, res) {
    res.locals.handler.json();
    
    // or
    (new response({ res }).json();
}

module.exports = {
    authenticate
}

Returning a JSON HTTP Response.

If the API response does not have an Express Response then it will return the JSON rather than outputting it as a HTTP response.

Accessing the JSON object

If you still need access the JSON object, then you can call object instead.

// ./routes/auth

function authenticate(_, res) {
    const object = res.locals.handler.object();
}

module.exports = {
    authenticate
}

Accessing the JSON object even when a Express Response is set.

Creating successful responses

After creating an instance of Response you are then able to call success which as the following default response:

{
	"status": 200,
	"success": true,
	"error": null,
	"data": null,
	"meta": null
}

Or you can pass in an object (each property is optional):

const success = {
	data, meta, status
};

const object = (new response).success(success).json();

Returning a successful response.

Creating unsuccessful responses

After creating an instance of Response you are then able to call error which as the following default response:

{
	"status": 400,
	"success": false,
	"error": null,
	"data": null,
	"meta": null
}

Or you can pass in an object (each property is optional):

const error = {
	error, status
};

const object = (new response).error(error).json();

Returning a unsuccessful response.

Setting individual properties

If for whatever reason you need to set specific properties (status, data, meta) after initially setting what type of response, then you can do:

const r = (new response).success();

// Overwriting the default status
r.status(201);

// Overwriting the default data
r.data('abc');

// Overwriting the default meta
r.meta({})

// JSON object with new data
const object = r.json();

Overwriting default success object then returning it.