express-final-response
The very last middleware of Express to handle every response of every request.
Features
- Both json-type response and HTML-type response (with some Express template engine) are supported at the same time
- Log every request while neccessary, and the logger is decided by you
- Every response has consistent and standard format, and also you are highly encouraged to customize yours for every distinct response
- Well tested
Usage
Installation
yarn add express-final-response
or npm install express-final-response --save
;
Work with Express
Let me just give you a clear example:
const path = ;const express = ;const routes = ;const finalResp = ; const app = ; // set Express template engine if you need toapp; // mount routes, see https://github.com/Maples7/express-mount-routes; // a very easy-to-write 404 middleware thank to this awesome packageapp; // aha, finally we got the protagonistapp; app;
API
// right value of `=` is default value and all parameters are optionalapp;
Response in each request
Once you want to return result in each request, call Express next()
function (Actually, we are making use of error handling mechanism of Express) with proper params.
If response is an error, thare are several ways to do this:
// pass an Error and `error` status would be applied automatically.; // pass an object but `msg` is an error, then `DBError` status would be applied and returned `msg` would be 'TrueError'. This way is better than former one because it can pass more infomation.; // the view would be applied so an HTML page would be returned. If there is no view param, a JSON would be returned like above, and the infomation in the returned JSON could be used while rendering view template.;
Response object would contain statusCode
, successful
, code
, status
, desc
, msg
, ext
(when isDebug
switch is on), for example:
statusCode: 500 successful: false code: 2 status: 'error' desc: 'Request Error!' msg: 'I am an error' ext: '...' // the whole Error object
If response is normal data, do it like this:
// the most ordinary way, `ext` is optional. `msg` here is designed for holding real data you want to return to clients or front-ends.; // the view would be applied so an HTML page would be returned. If there is no view param, a JSON would be returned like above, and the infomation in the returned JSON could be used while rendering view template.;
Response object would contain statusCode
, successful
, code
, status
, desc
, msg
(optional), ext
(optional), for example:
statusCode: 200 successful: true code: 0 status: 'success' desc: 'Request Successful!' msg: 'test 1 success'
See default statuses definition
There are so many other defaut statuses you can choose besides success
and error
, make sure to see ./lib/status.json to catch them. Of course, you can define your own statuses for the benefit of customStatus
parameter like what I have told you.
You are welcomed to review test.js, controllers dir and views dir in this project for more information of usage.