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

express-response-formatter

v2.0.2

Published

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/aofleejay/express-response-formatter/blob/master/LICENSE.md) [![npm](https://img.shields.io/npm/v/express-response-formatter.svg)](https://www.npmjs.com/package/expr

Downloads

1,901

Readme

express-response-formatter

GitHub license npm code style: prettier PRs Welcome Build Status Coverage Status

Better way to format Express response

How It Works

  • You can use response with readable name like res.formatter.ok for 200 ok or res.formatter.badRequest for 400 bad request.
  • It will format your response in two ways success and error.
    • If response is 2xx, 3xx return response under object key data.
    • If response is 4xx, 5xx return response under object key error.
  • You can pass metadata as second parameter and it's will present under meta object key.

Usage

Installation

npm install express-response-formatter --save

Quick Start

Response for 200 Ok.

import app from 'express'
import { responseEnhancer } from 'express-response-formatter'

const app = express()

// Add formatter functions to "res" object via "responseEnhancer()"
app.use(responseEnhancer())

app.get('/success', (req, res) => {
  const users = [{ name: 'Dana Kennedy' }, { name: 'Warren Young' }]

  // It's enhance "res" with "formatter" which contain formatter functions
  res.formatter.ok(users)
})

app.listen(3000, () => console.log('Start at http://localhost:3000'))

Result

HTTP/1.1 200 Ok
{
  "data": [
    {
      "name": "Dana Kennedy"
    },
    {
      "name": "Warren Young"
    }
  ]
}

More usages

Response for 200 Ok with meta field

app.get('/success-with-meta', (req, res) => {
  const users = [{ name: 'Dana Kennedy' }, { name: 'Warren Young' }]

  const meta = {
    total: 2,
    limit: 10,
    offset: 0,
  }

  res.formatter.ok(users, meta)
})
HTTP/1.1 200 Ok
{
  "meta": {
    "total": 2,
    "limit": 10,
    "offset": 0,
  },
  "data": [
    {
      "name": "Dana Kennedy"
    },
    {
      "name": "Warren Young"
    }
  ]
}

Response for 400 Bad Request with "error"

app.get('/bad-request', (req, res) => {
  const errors = [
    { detail: 'Field id is required.' },
    { detail: 'Field foo is required.' },
  ]

  res.formatter.badRequest(errors)
})
HTTP/1.1 400 Bad Request
{
  "error": [
    {
      "detail": "Field id is required."
    },
    {
      "detail": "Field foo is required."
    }
  ]
}

APIs

| METHOD | STATUS CODE | | ---------------------------------------------- | ----------- | | res.formatter.ok(data, meta?) | 200 | | res.formatter.created(data, meta?) | 201 | | res.formatter.accepted(data, meta?) | 202 | | res.formatter.noContent(data, meta?) | 204 | | res.formatter.badRequest(errors, meta) | 400 | | res.formatter.unauthorized(errors, meta) | 401 | | res.formatter.forbidden(errors, meta) | 403 | | res.formatter.notFound(errors, meta) | 404 | | res.formatter.methodNotAllowed(errors, meta) | 405 | | res.formatter.timeout(errors, meta) | 408 | | res.formatter.conflict(errors, meta) | 409 | | res.formatter.unprocess(errors, meta) | 422 | | res.formatter.tooManyRequests(errors, meta) | 429 | | res.formatter.serverError(errors, meta) | 500 | | res.formatter.badGateway(errors, meta) | 502 | | res.formatter.serviceUnavailable(errors, meta) | 503 | | res.formatter.gatewayTimeout(errors, meta) | 504 |