diff --git a/docs/reference/sails.config/sails.config.http.md b/docs/reference/sails.config/sails.config.http.md index 4c81886b38..7cb4d66041 100644 --- a/docs/reference/sails.config/sails.config.http.md +++ b/docs/reference/sails.config/sails.config.http.md @@ -12,6 +12,7 @@ Configuration for your app's underlying HTTP server. These properties are conve `cache` | ((number)) | `31557600000` _(1 year)_ | The number of milliseconds to cache [static assets](https://sailsjs.com/documentation/concepts/assets) when your app is running in a ['production' environment](https://sailsjs.com/documentation/reference/configuration/sails-config#?sailsconfigenvironment).
More specifically, this is the "max-age" that will be included in the "Cache-Control" header when responding to requests for static assets—i.e. any flat files like images, scripts, stylesheets, etc. that are served by Express' static middleware. `serverOptions` | ((dictionary)) | `{}` | _SSL only_: advanced options to send directly to the [Node `https` module](https://nodejs.org/dist/latest/docs/api/https.html) when creating the server. These will be merged with your [SSL settings](https://sailsjs.com/documentation/reference/configuration/sails-config#?sailsconfigssl), if any. See the [createServer docs](https://nodejs.org/dist/latest/docs/api/https.html#https_https_createserver_options_requestlistener) for more info. `trustProxy` | ((boolean)) _or_ ((function)) | `undefined` | This tells Sails/Express how it should interpret "X-Forwarded" headers. Only use this setting if you are using HTTPS _and_ if you are deploying behind a proxy (for example, a PaaS like Heroku). If your app does not fit that description, then leave this as undefined. Otherwise, you might start by setting this to `true`, which works for many deployments. If that doesn't work, see [here](https://expressjs.com/en/guide/behind-proxies.html) for all available options. + `queryParser` | ((boolean)) _or_ ((string)) _or_ ((function)) | `undefined` | This tells Sails/Express how it should parse query strings. See "query parser" [here](https://expressjs.com/en/4x/api.html#app.settings.table) for more information. ### Customizing the body parser diff --git a/lib/hooks/http/index.js b/lib/hooks/http/index.js index 5d09db202d..8f599ff0cb 100644 --- a/lib/hooks/http/index.js +++ b/lib/hooks/http/index.js @@ -87,6 +87,9 @@ module.exports = function(sails) { // (this is passed in to Express as the "trust proxy" setting) trustProxy: false, + // (this is passed in to Express as the "query parser" setting) + queryParser: undefined, + }//< .http> },//< / defaults > diff --git a/lib/hooks/http/initialize.js b/lib/hooks/http/initialize.js index 6afa01dfe2..58413ac50d 100644 --- a/lib/hooks/http/initialize.js +++ b/lib/hooks/http/initialize.js @@ -169,6 +169,10 @@ module.exports = function(sails) { expressApp.set('trust proxy', sails.config.http.trustProxy); } + if (sails.config.http.queryParser !== undefined) { + expressApp.set('query parser', sails.config.http.queryParser); + } + // Whenever Sails binds a route, bind it to the internal Express router. sails.on('router:bind', function(route) { // Clone the route so that if a route handler messes with the options, the changes diff --git a/test/hooks/http/initialize.test.js b/test/hooks/http/initialize.test.js index 18ca70e06c..73f8b7c766 100644 --- a/test/hooks/http/initialize.test.js +++ b/test/hooks/http/initialize.test.js @@ -91,4 +91,43 @@ describe('HTTP hook', function (){ }); + describe('with custom queryParser config', function() { + + var app; + before(function(done) { + app = Sails(); + app.lift({ + globals: false, + loadHooks: [ + 'moduleloader', + 'userconfig', + 'http' + ], + log: {level: 'silent'}, + http: { + queryParser: false + }, + routes: { + 'get /': function(req, res) {return res.send(req.query);} + }, + port: 1343 + }, done); + }); + + it('should be able to respond to requests using the custom queryParser', function(done) { + request.get('http://localhost:1343?test=123', function(err, res, body) { + if (err) { return done(err); } + try { + assert.deepEqual(JSON.parse(body), {}); + } + catch (e) {return done(e);} + return done(); + }); + }); + + after(function(done) { + app.lower(done); + }); + }); + });