Note that there are some explanatory texts on larger screens.

plurals
  1. POExpress/Connect: Unable to parse body after making call to redis
    primarykey
    data
    text
    <p>I am trying to shorten my Express/Connect middleware pipeline by only calling certain middleware functions based on the requested path.</p> <p>However, the following will fail:</p> <pre><code>_cookieParser(req, res, function(err) { if(err) return next(err); _session(req, res, function(err) { if(err) return next(err); _csrf(req, res, function(err) { if(err) return next(err); loadUserFromSession(req, res, function(err) { if(err) return next(err); if(req.method == "POST") { _bodyParser(req, res, next); } else { next(); } }); }); }); }); </code></pre> <p>But this will work fine:</p> <pre><code>_cookieParser(req, res, function(err) { if(err) return next(err); _session(req, res, function(err) { if(err) return next(err); _csrf(req, res, function(err) { if(err) return next(err); _bodyParser(req, res, function(err) { if(err) return next(err); loadUserFromSession(req, res, next); }); }); }); }); </code></pre> <p>Where loadUserFromSession is:</p> <pre><code>function loadUserFromSession(req, res, next) { if(req.session &amp;&amp; req.session.userId) { userFunctions.getUserById(req.session.userId, function(err, user) { if(err) return next(err); if(user) { req.user = user; return next(); } else { req.session.destroy(); return next(new Error('Unauthenticated')); } }); } else { return next(new Error('Unauthenticated')); } }; </code></pre> <p>Why can I not call bodyParser() after loadUserFromSession()?</p> <p><strong>EDIT</strong></p> <p>Sorry for the lack of detail on the failure/unexpected outcome.</p> <p>If I put bodyParser() or just json() (since the POST content is json) after loadUserFromSession(), the calls never return inside of json(). If I put breakpoints in node inspector on either res.on('data') or res.on('end') neither get tripped.</p> <p>The source of the json middleware is below:</p> <pre><code>exports = module.exports = function(options){ var options = options || {} , strict = options.strict !== false; var limit = options.limit ? _limit(options.limit) : noop; return function json(req, res, next) { if (req._body) return next(); req.body = req.body || {}; if (!utils.hasBody(req)) return next(); // check Content-Type if ('application/json' != utils.mime(req)) return next(); // flag as parsed req._body = true; // parse limit(req, res, function(err){ if (err) return next(err); var buf = ''; req.setEncoding('utf8'); req.on('data', function(chunk){ buf += chunk &lt;==BREAKPOINT NEVER GETS CALLED }); req.on('end', function(){ var first = buf.trim()[0]; &lt;==BREAKPOINT NEVER GETS CALLED if (0 == buf.length) { return next(400, 'invalid json, empty body'); } if (strict &amp;&amp; '{' != first &amp;&amp; '[' != first) return next(400, 'invalid json'); try { req.body = JSON.parse(buf, options.reviver); next(); } catch (err){ err.body = buf; err.status = 400; next(err); } }); }); } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload