Express.js middleware abstracting error vs success responses.
$ npm install express-responder --save
- Middleware control flow
- Uniformed error handling
- Request validation
- Response validation
- Content-Type repsonse negotiation
The rationale behind this module is to reduce the amount of repetitive code required to validate a request cycle in express.js/ connect. Ever find yourself writing code like this?
We were tired of seeing this throughout our express.js apps so this module was born, converting the above into this:
var responder = ;var express = ;var app = ;app;// ... Other middleware like session and staticapp;// ... After all your routesapp;
Register the middleware exposing the methods below on request and response objects.
Register middleware to handle 404 and Error responses, along with content negotiation.
Change the querystring parameter used to detect a JSONP request.
Pass custom callbacks for each content-type. Supports
Check the request for errors using either express-validator methods or pass your own custom errors as the first parameter. If an error is found, the response will be an
Http400Error.var users =1:books:'Growth Engines''It\'s Not About The F-Stops'2:books:'Design For How People Learn''Smart Information Systems';app;
Convenience method that returns
trueif there is data and there is no error,
falseotherwise. If you need to check for an error and still do more before actually responding.app;
Responds if an error was passed and return
trueotherwise just return
falseallowing for further processing inside middleware.app;
res.continueOrError(err, data, next)
Continue onto next matching middleware saving
res.locals.responseor respond with
Explicitly continue onto next matching middleware saving
Debugging is implemented using the debug module.
$ DEBUG=express-responder npm start
processenvDEBUG = 'express-responder'
"Bernard of Chartres used to compare us to [puny] dwarfs perched on the shoulders of giants. He pointed out that we see more and farther than our predecessors, not because we have keener vision or greater height, but because we are lifted up and borne aloft on their gigantic stature." Quoted From
This module is primarily built on:
and all of the other modules that make up these packages.
(The MIT License) Copyright (c) 20014 Big Mountain Ideas + Innovations <firstname.lastname@example.org> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.