Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

expressjs-response

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expressjs-response

Express middleware to generate JSON response

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Expressjs Response GitHub license npm

Express middleware to generate JSON response

Installation

npm install expressjs-response --save

Quick Start

Use as Express middleware

import express from 'express'
import responseEnhancer from 'expressjs-response'

const app = express()

// use in express middleware
app.use(responseEnhancer({
  withStatusCode: true, // Include status code in response body.
  withStatusMessage: true, // Include status message in response body.
}))

// example usage
app.get('/success', (req, res) => res.ok({ name: 'John Doe' }))
app.get('/badrequest', (req, res) => res.badRequest('Invalid parameter.'))
app.get('/badgateway', (req, res) => res.badGateway())

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

Example Response

200
res.ok({ name: 'John Doe' })
HTTP/1.1 200 Ok
{
    "status": "success",
    "data": {
        "name": "John Doe"
    }
}
400
res.badRequest()
HTTP/1.1 400 Bad Request
{
    "status": "fail",
    "error": {
        "code": "400",
        "message": "Bad Request"
    }
}
400 With Parameter
res.badRequest('Invalid parameter.')
HTTP/1.1 400 Bad Request
{
    "status": "fail",
    "error": {
        "code": "400",
        "message": "Bad Request",
        "detail": "Invalid parameter."
    }
}
502
res.badGateway()
HTTP/1.1 502 Bad Gateway
{
    "status": "error",
    "error": {
        "code": "502",
        "message": "Bad Gateway"
    }
}

API

METHODCODEPARAMS
res.ok()200res.ok(data)
res.created()201res.created(data)
res.noContent()204-
res.badRequest()400res.badRequest(error)
res.unauthorized()401res.unauthorized(error)
res.forbidden()403res.forbidden(error)
res.notFound()404res.notFound(error)
res.methodNotAllowed()405res.methodNotAllowed(error)
res.unprocessableEntity()422res.unprocessableEntity(error)
res.internalServerError()500res.internalServerError(error)
res.badGateway()502res.badGateway(error)
res.serviceUnavailable()503res.serviceUnavailable(error)
res.gatewayTimeout()504res.gatewayTimeout(error)

FAQs

Package last updated on 12 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc