@obi-tec/express-response-models
v1.1.2
Published
A simple library to manage response (success or error) using express library
Downloads
658
Readme
Express Response Models
🏁 Content
Install
npm install @obi-tec/express-response-models
See all tags clicking here.
Usage
In your main root file, where you import express library, you must set the following settings in middleware:
const { response, ErrorHttp } = require('@obi-tec/express-response-models');
// Add function success to express response object
app.use(function(req, res, next) {
res.success = (body, statusCode = 200, headers = null, cache = 0) => {
return response.success(res, body, statusCode, headers, cache);
};
res.ErrorHttp = ErrorHttp;
next();
});
// Handler to ErrorHttp Class
app.use((err, request, response, next) => {
if (err instanceof ErrorHttp) {
return response.status(err.httpStatusCode).json({
message : err.message,
code : err.businessStatusCode
});
}
return response.status(500).json({
message : 'Internal server error',
code : 'internal-server-error'
});
});
//
After declaring in your main file, you are able to use this library to respond to:
- A success request
return res.success(body, 200);
- An error request
throw new res.ErrorHttp('Error message', 400, 'api-name-400_error-message'); // OR just throw new ErrorHttp('Error message', 400, 'api-name-400_error-message');