Monday 15 April 2013

javascript - exit from chain of route specific middleware in express/ nodejs -


I have a series of "root specific middleware" for this route, such as:

 < Code> WAR Express = expected ('Express'); Var server = express (); Var mw1 = function (req, resp, next) {// do stuff if (success) {next (); } And {req.connection.destroy (); // next without calling ()}}; Var mw2 = function (rik, hop, next) {// dot dot (success) {next (); } And {req.connection.destroy (); // next without calling ()}}; Server.post ('/ some / path', [mw1, mw2], function (req, resp) {// write response}); Typical middleware for   

[mw1, mw2] route / some / path

This is separate from server-wide middleware:

  server.use (mw1); Server.use MW2 ();   

This applies to all routes defined.

Now my problem is that I want to get out of the series, i.e. If success lies in mw1 , then I do not want to call mw2 if success in Mw2 , then I am called without interruption function. Currently, both mw1 and mw2 are being called both or not next () is called - and I do not know why .

How can I go about doing this?

You can call next to 'code'), as said: >

example
  var express = expected ('express'), app = express (); // Function names do not work on app post ('/ some / path', middleware 1, middleware 2, function path 1 (rike, race, next)). App.all ('*', function routing 2 (reeck, race, next) {// doing something that handles 404 requests}); App.use (function error handler (mistake, rick, race, next) {// handle error}); Function Middleware 1 (rake, race, next) {// ... if (! Success) {// Bypass to Middleware 2 and route 1, route 2 will be called return next ('route'); } // call middleware2 next (); } // To clear things along with knowingly Middleware 1, Middleware 2 (Reich, Race, Next) {if (success!) Bypass path 1 and route 2 / Errorhandler will be recalled with error returns (Error ('Middleware 2 Fail')); } // call route 1 next (); }    

No comments:

Post a Comment