2.2.0 • Published 2 years ago

good-status v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

good-status

Build Status check Coverage Status

npm package npm package npm package npm package

Simple express middleware for sending standard status response.

Install

# NPM
npm i good-status --save

Usage

import and setup

var goodStatus = require("good-status");
app.use(goodStatus());

This adds extra functions to the response object, for handling responses with accurate status code.

Example

Send response with valid status code by using the methods

app.post("/", (req, res) => {
  res.created({
    status: "success",
    data: { name: "John Doe" },
  }); // { status: "success", data: {name: "John Doe"} }, 201
});

app.get("/user", (req, res) => {
  res.notFound(); // 404
});

Options

OptioinsDefaultDescription
sendfalseThis returns the response object to the user for further use
unofficialfalseThis specifies the use of unoficial status code
extendfalseWhen true all good-status response function will be populated to the response
nginxfalseWhen true, all status codes for Nginx will be available
infoServicefalseWhen true, all status codes for Microsoft's Internet Information Services (IIS) will be available
cloudflarefalseWhen true, all status codes for Cloudflare will be available

For more information on the status codes, please refer to HTTP Status Code

To send response body manually

app.get("/admin", (req, res) => {
  const data = { msg: "require authentication" };
  res.goodStatus.unauthorized().json(data); // { msg: "require authentication" }, 401
});

To add the response functions to the response object

app.get("/admin", goodStatus({ extend: true }), (req, res) => {
  const data = { msg: "require authentication" };
  res.unauthorized().json(data);
});

// OR

app.use(goodStatus({ extend: true }));
app.get("/admin", (req, res) => {
  const data = { msg: "require authentication" };
  res.unauthorized().json(data);
});

To have access to unofficial status codes you have to enable them when initialazing the middleware

app.use(goodStatus({ unofficial: true }));

To send response with custom status code function

app.use(goodStatus({ send: true }));
app.get("/admin", (req, res) => {
  res.goodStatus.unauthorized({ msg: "require authentication" });
});

app.use(goodStatus({ send: true, extend: true }));
app.get("/test", (req, res) => {
  res.unauthorized({ msg: "require authentication" });
});

Available methods

All OFFICIAL and UNOFFICIAL status codes are available for use

Note:

To use the unofficial status code, you must specify it while initializing

2.2.0

2 years ago

2.1.0

2 years ago

2.0.0

3 years ago

1.0.0

3 years ago