Is there a way in sails.js to convert camelCase to snake_case while returning the JSON response using res.ok(data), where data is an object, without having explicit getters or setters in the model to do this?
There is also another solution that don't need to be implemented in each response.
You can define your custom hook in api/hooks folder with following content:
var snakeCase = require('snake-case');
module.exports = function (sails) {
return {
routes: {
after: {
'all /*': function overrideJsonx(req, res, next) {
var jsonx = res.jsonx;
res.jsonx = function (obj) {
var res = snakeCase(obj);
jsonx(res);
};
next();
}
}
}
}
};
It will work for all responses without modifying custom responses in api/response folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With