Himmators Himmators - 1 year ago 62
Node.js Question

Load specific middlewear instead of using next();

I have a few route like this:

app.post('/user/me', c.auth, c.striper.make, c.user.edit, c.user.requestDriver, render);
app.post('/user/:id', c.auth, c.targetUser, c.user.makeDriver, c.user.makeAdmin, render);

When the validation fails, I want to skip all middlewear and go run the render-function, with req, res I can't use
for this because I don't know where in the order I am, is there some other way to call a specific function with the parameters
res, req and next();
(my validation doesn't have access to those parameters so I can't just do

render(res, req, next)

Maybe some way to replace the middlewear setup in routes.

Answer Source

I suggest to call next(new Error(smth-data)); and render in Error-middleware with 4 params. Or you want special render for each route?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download