express
express copied to clipboard
Setting query parser after registering middleware causes parser setting to be ignored.
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"}');
});
});
});
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.
Thanks for the quick reply!