Skip to content

Instantly share code, notes, and snippets.

@crobinson42
Created February 13, 2016 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crobinson42/3ba216ba54997080fcad to your computer and use it in GitHub Desktop.
Save crobinson42/3ba216ba54997080fcad to your computer and use it in GitHub Desktop.
Add custom param to each request in Sails.js
/**
* HTTP Server Settings
* (sails.config.http)
* sails_project/config/http.js
*
* Sails.js v.11.*
*/
module.exports.http = {
middleware: {
/***************************************************************************
* *
* The order in which middleware should be run for HTTP request. (the Sails *
* router is invoked by the "router" middleware below.) *
* *
***************************************************************************/
order: [
'startRequestTimer',
'cookieParser',
'session',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'myRequestLogger',
'router',
'www',
'favicon',
'404',
'500'
],
/****************************************************************************
* *
* Example custom middleware; logs each request to the console. *
* *
****************************************************************************/
myRequestLogger: function (req, res, next) {
// see the requests in the console for DEV purposes
console.log("Requested :: ", req.method, req.url);
if (req.body) {
if (req.body.where) {
_.merge(req.body.where, {myCustomParam : 'value' });
}
else {
_.merge(req.body, {myCustomParam : 'value' });
}
}
if (req.params) {
if (req.params.where) {
_.merge(req.params.where, {myCustomParam : 'value' });
}
else {
_.merge(req.params, {myCustomParam : 'value' });
}
}
if (req.query) {
if (req.query.where) {
var where = JSON.parse(req.query.where);
_.merge(where, {myCustomParam : 'value' });
req.query.where = JSON.stringify(where);
}
else {
_.merge(req.query, {myCustomParam : 'value' });
}
}
return next();
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment