Expressjs Response
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 'exressjs-response'
const app = express()
app.use(responseEnhancer())
app.get('/success', (req, res) => res.ok(
{ name: 'John Doe' }
))
app.get('/badrequest', (req, res) => res.badRequest())
app.get('/unauthorized', (req, res) => res.unauthorized())
app.listen(3000, () => console.log('Start at http://localhost:3000'))
Example Response
2xx
res.ok({ name: 'John Doe' })
{
"status": "success",
"data": {
"name": "John Doe"
}
}
4xx
res.badRequest()
{
"status": "fail",
"message": "Bad Request"
}
5xx
res.badGateway()
{
"status": "error",
"message": "Bad Gateway"
}
API
METHOD | CODE | PARAMS |
---|
res.ok() | 200 | res.ok(data) |
res.created() | 201 | res.created(data) |
res.noContent() | 204 | - |
res.badRequest() | 400 | - |
res.unauthorized() | 401 | - |
res.notFound() | 404 | - |
res.methodNotAllowed() | 405 | - |
res.unprocessableEntity() | 422 | - |
res.internalServerError() | 500 | - |
res.badGateway() | 502 | - |
res.serviceUnavailable() | 503 | - |
res.gatewayTimeout() | 504 | - |