node.js - Restify Middleware - correctly calling next middleware in stack -
i using restify nodejs , have question on correct way of returning control next middleware in stack. hope using correct phrase when "next middleware in stack".
basically, code looks this:
//server server created using restify server.use(function (req, res, next) { //if checks success return next(); });
now, wish know should code return next();
or should next();
pass control next in stack?
i checked , both work - both pieces of code pass control , return data expected - wish know if there difference between 2 , if need use 1 on another.
there's no difference. took @ restify source, , doesn't seem return value of middleware @ all.
the reason using return next()
purely matter of convenience:
// using this... if (somecondition) { return next(); } res.send(...); // instead of... if (somecondition) { next(); } else { res.send(...); };
it might prevent errors this:
if (somecondition) next(); res.send(...); // !!! oops! called next middleware *and* we're // sending response ourselves!
Comments
Post a Comment