Created
October 4, 2012 22:09
-
-
Save rajaraodv/3836795 to your computer and use it in GitHub Desktop.
This example shows how to create multiple error-validator-middlewares for your Express app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Say you want to add multiple validation middlewares to Express app, this is an example of how to do it. | |
First create 2 middlewares: first one(e.g. validateRequest1) to validate and the 2nd one to handle errors (e.g validateRequest1Handler). | |
If you want to add a 2nd validation middleware, create two more middlewares (validateRequest2 & validateRequest2Handler). | |
Note - Error-handling-middleware v/s non-error-handling-middleware | |
Error handler middleware must take 4 parameters(err, req, res, next) including "err" as first parameter. Where as are normal middlewares only take 3(req, res, next). | |
*/ | |
//This is how it would look like.. | |
var express = require('express'); | |
var app = express(); | |
app.use(validateRequest1); | |
app.use(validateRequest1Handler); //only called if validateRequest1 throws error | |
app.use(validateRequest2); | |
app.use(validateRequest2Handler); // only called if validateRequest2 throws error | |
/* Control-flow | |
Case 1: validateRequest1 throws error and validateRequest1Handler handles & returns response: | |
Now, if validateRequest1 throws an error, validateRequest1Handler is called. But if validateRequest1 doesn't throw error, control jumps to the next non-error-handling-middleware (validateRequest2 in the above case). | |
- validateRequest1Handler should act like the last handler.. so you should return response here itself. | |
Case 2:validateRequest1 throws error and validateRequest1Handler also throws error. | |
- If validateRequest1Handler also throws error, control will directly jump to next error-error-handler "validateRequest2Handler" ( & not to next middleware "validateRequest2"). | |
Case 3: validateRequest1 doesn't throw error but validateRequest2 throws error. | |
Similar to case 1, control will go from validateRequest2 to next error-handler-middleware "validateRequest2Handler" | |
- validateRequest2Handler should act like the last handler.. so you should return response here itself. | |
*/ | |
function validateRequest1(req, res, next) { | |
// throw err; //control goes to validateRequest1Handler | |
// next(); //control goes to validateRequest2 | |
} | |
function validateRequest1Handler(err, req, res, next) { | |
//res.send("error"); // response will be sent back to browser and ends request. | |
//throw err; //control goes to validateRequest2Handler | |
} | |
function validateRequest2(req, res, next) { | |
// throw err; //goes to validateRequest2Handler | |
// next(); // goes to your app | |
} | |
function validateRequest2Handler(err, req, res, next) { | |
// res.send("error"); // response will be sent backt to browser and ends. | |
//throw err; // If this is the last error handler, your app will crash. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment