2.2.0 • Published 2 years ago
express-standard-http-responses v2.2.0
Foreword
Hi 👋 !
This package has the only purpose to format the responses for express.
Installation
npm install express-standard-http-responses --save
Environment used
npm@8.5.1
node@14.16.0
Example
import {httpResponses} from "express-standard-http-responses";
import * as express from 'express';
const app = express();
const port = process.env.port || 3000;
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`)
});
app.get('/test', (req, res) => {
return httpResponses(res).body({body: {nested: "content"}}).message('Information').created();
});
Response example
{
"body": {
"nested": "content"
},
"message": "Information"
}
Headers
You can set headers manually or through middlewares
app.get('/test', (req, res) => {
res.header('Authorization', 'test');
return httpResponses(res).body({body: {nested: "content"}}).message('Information').created();
});
Methods
Setting a body
httpResponses(res).body({test: 'body'});
Setting a message
httpResponses(res).message('This is an example.');
Codes 2xx
Method | Default message response |
---|---|
success | none |
created | none |
accepted | none |
noContent | none |
Codes 3xx
Method | Default message response |
---|---|
multipleChoices | none |
movedPermanently | none |
found | none |
seeOther | none |
Codes 4xx
Method | Default message response |
---|---|
badRequest | none |
unauthorized | You are not authorized to access this ressource. |
forbidden | Access is forbidden. |
notFound | This item was not found. |
methodNotAllowed | This method is not allowed for this ressource. |
proxyAuthRequired | A proxy authentication is required. |
requestTimeout | The client did not produce a request within the time that the server was prepared to wait. |
conflict | There is a conflict on the requested ressource. |
gone | This ressource is no longer available. |
lengthRequired | Content length must be specified for this ressource. |
teapot | I am a teapot. |
misdirected | This server is not able to produce a response for the given request. |
unprocessable | The request was well-formed but was unable to be followed due to semantic errors. |
locked | This ressource has been locked. |
tooManyRequests | You have sent too many requests in a given amount of time. |
Codes 5xx
Method | Default message response |
---|---|
internalError | An error occurred, try again later or report it to the developers team. |
notImplemented | This ressource is not implemented. |
serviceUnavailable | The service is not available at this time, try again later. |
insufficientStorage | Unable to store the representation needed for to complete the request. |
loopDetected | A loop was detected and the request has been aborted. |