status-parser
v1.0.9
Published
Express.js HTTP friendly responses handler
Downloads
2
Maintainers
Readme
Status Parser
Express.js HTTP friendly responses handler
Getting Started 🚀
Installation
copy and paste this code into the terminal
npm install status-parser --save
Examples
send response 201 on success
const router = require('express').Router()
const status = require('status-parser')
router.get('/', (req,res)=>{
status.success(res).Created() // => {"status":201,"message":"resource created","data":"resource created"}
})
send error response 400
const router = require('express').Router()
const status = require('status-parser')
router.get('/', (req,res)=>{
status.clientError(res).BadRequest('this is an error message') // => {"status":400,"message":"bad request","error":"this is an error message"}
})
add personalizated data to 202 response
const router = require('express').Router()
const status = require('status-parser')
router.get('/', (req,res)=>{
const data = {
"friends": {}
}
status.success(res).Accepted(data) // => {"status":202,"message":"request accepted","data":{"friends":{}}}
})
add personalizated headers to 200 response
const router = require('express').Router()
const status = require('status-parser')
router.get('/', (req,res)=>{
const data = {
"friends": {}
}
const options = {
header: {
"Content-Type": "application/json"
}
}
status.success(res,options).OK(data)
})
add personalizated cookie to 200 response
const router = require('express').Router()
const status = require('status-parser')
router.get('/', (req,res)=>{
const data = {
"friends": {}
}
const options = {
header: {
"Content-Type": "application/json"
},
cookie: {
"name": "token",
"val": "secret",
options:{
"httpOnly":true
}
}
}
status.success(res,options).OK(data)
})
return response 200 object
const status = require('status-parser')
const response = status.success().OK()
console.log(response)// => {"status":200,"message":"successful request","data":"successful request"}
all response status from 1xx to 5xx
const status = require('status-parser')
const methods = {
"1xx": status.information(),
"2xx": status.success(),
"3xx": status.redirect(),
"4xx": status.clientError(),
"5xx": status.serverError()
}
real world example
const router = require('express').Router()
const status = require('status-parser')
const {User} = require('./entities/user.js')
router.post('/sign-in', (req,res)=>{
const {email,password} = req.body
new User()
.signIn(email,password)
.then(jwt => {
const options = {
cookie: {
"name": "token",
"val": jwt,
options:{
"httpOnly":true
}
}
}
status.success(res,options).OK('signed in')
})
.catch(error => {
status.clientError(res).BadRequest(error.message)
})
})
Author ✒️
- Bruno Passarelli - brunopass
Licence 📄
this project is licensed under the MIT license