SailsJS: How to convert camel case to snake case to JSON response implicitly?

Is there any way sails.js convert camelCase to snake_case by returning a JSON response using res.ok(data) , where the data is an object without having explicit getters or setters in the model for this?

+5
source share
1 answer

There is also another solution that does not need to be implemented in each answer.

You can define your own hook in the api/hooks folder with the following contents:

 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 answers without changing the user answers in the api/response folder.

+4
source

Source: https://habr.com/ru/post/1233964/


All Articles