Skip to content

adamastern/api-response-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

api-response-middleware

Express.js response formatting middleware

Installation

$ npm install api-response-middleware

API

var responseMiddleware = require('api-response-middleware');
app.use(responseMiddleware(options)); //App is an Express.js app. Pass in options

Options

The function takes an option options object that may contain any of the following keys:

failCodes

An array of HTTP status codes that should be treated as 'fail', instead of 'error' (more on this below)

Response formats

This middleware attaches a few new methods to the Express 'res' object. These methods will send a response in one of three formats - Success, Fail, or Error

Succcess format:
{
    "status":"success",
	"payload":{}
}
Fail format:
{
    "status":"fail",
	"message":"Oops!"
}
Error format:
{
    "status":"error",
	"message":"Oops!"
}

Methods

res.apiResponse(response)

res.apiError(err, message, code, forceFail)

res.apiNotFound(err, message)

res.apiNotAllowed(err, message)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published