Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/sails.config/sails.config.http.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).<br/>More specifically, this is the "max-age" that will be included in the "Cache-Control" header when responding to requests for static assets&mdash;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
Expand Down
3 changes: 3 additions & 0 deletions lib/hooks/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 >
Expand Down
4 changes: 4 additions & 0 deletions lib/hooks/http/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/hooks/http/initialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

});