Created
February 13, 2016 04:23
-
-
Save crobinson42/3ba216ba54997080fcad to your computer and use it in GitHub Desktop.
Add custom param to each request in Sails.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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