express icon indicating copy to clipboard operation
express copied to clipboard

Setting query parser after registering middleware causes parser setting to be ignored.

Open matthewloring opened this issue 11 years ago • 2 comments

When I register middleware with app.use before setting the query parser to simple, my app appears to use the extended query parser. Is there a contract that all calls to app.set must occur before the first middleware is registered?

Code (this can be made to pass by removing the first call to app.use):

var http = require('http');

var express = require('express');
var app = express();
app.use(function(req, res, next) { next(); });
app.set('query parser', 'simple');
app.use(function(req, res) { res.send(req.query); });

var server = app.listen(8080, function(err) {
  http.get('http://localhost:8080/?user[name]=tj', function(res) {
    var response = '';
    res.on('data', function(chunk) {
      response += chunk.toString();
    });
    res.on('end', function() {
      server.close();
      require('assert').equal(response, '{"user[name]":"tj"}');
    });
  });
});

matthewloring avatar Jul 31 '15 23:07 matthewloring

Is there a contract that all calls to app.set must occur before the first middleware is registered?

Yep, this is exactly correct: in Express 4.x and below (perhaps we can change in in 5.x), you have to make these changes before your first route/middleware call.

dougwilson avatar Jul 31 '15 23:07 dougwilson

Thanks for the quick reply!

matthewloring avatar Aug 01 '15 00:08 matthewloring