The last middleware of express to handle the final response.
$ npm install express-last-middleware
- Uniformly use the
next()
function in express to handle the response - Setting the HTTP status code and response body at the same time
- Custom internal error response handler
First, use express-last-middleware
as the last middleware for express.
const express = require('express');
const lastMiddleware = require('express-last-middleware');
const app = express();
// some routes ...
app.use(lastMiddleware());
app.listen(3000);
Then, when you need to send a response, just pass a value to the next()
function. This passed value can be an object or a basic data type value.
app.get('/', (req, res, next) => {
next({ response: { data: 'OK' } });
});
If you pass a non-error object to the next()
function, this object allows three optional attributes.
app.get('/', (req, res, next) => next({
statusCode: 200, // set the HTTP status code, default 200
response: { data: 'OK' }, // set the response body
format: 'json' // set the format of the response body, 'json' or 'string', default 'json'
}));
If you pass an error object to the next()
function, the default HTTP status code for this response is 500
and the response body is an object containing the error message. You can also customize the function to handle the error object.
app.get('/', (req, res, next) => {
next(new Error('This is an error'));
});
If you pass a basic data type value to the next()
function, the default HTTP status code for this response is 200
and the response body is the value of this basic data type value.
app.get('/', (req, res, next) => {
next('OK');
});
This options
has two optional attributes, format
and handleError
.
The format of the response body, json
or string
, default json
.
The function to handle the error object. If this function is not set, a 500
HTTP status code and error message response body will be returned when the next()
function passes in an error object or an internal error occurs.
This function must receive three parameters, error
, req
and res
.
app.use(lastMiddleware({
format: 'json',
handleError: (error, req, res) => {
res.status(500);
res.json({ message: error.message });
}
}));
$ npm test