javascript - Express.js response converts snake_case keys to camelCase automatically - Stack Overflow

I'm working on a small project at work and we have an Express.js based node application running th

I'm working on a small project at work and we have an Express.js based node application running that sends a json response that has keys in snake_case format. We have another node application that consumes this service but the response object keys are accessed in camelCase format here. I'd like to know what happens in the background to make this work.

This is the code in the REST API

app.get('/api/customer/:id', (req, res) => {
    const data = {
        "arr": [{
             "my_key": "609968029"
        }]
    }
    res.send(data);
});

This is how it is consumed in the other node application

getData = (id) => {
  const options = {
    url: `api/customer/${id}`
  };

  return httpClient.get(options)
    .then(data => {
      const arr = data.arr.map(arrEntry => {
        return {
          myKey: arrEntry.myKey
        };
      });

      return {
        arr
      };
    });
};

Here myKey correctly has the data from the REST API but I'm not sure how my_key is converted to myKey for it work.

I'm working on a small project at work and we have an Express.js based node application running that sends a json response that has keys in snake_case format. We have another node application that consumes this service but the response object keys are accessed in camelCase format here. I'd like to know what happens in the background to make this work.

This is the code in the REST API

app.get('/api/customer/:id', (req, res) => {
    const data = {
        "arr": [{
             "my_key": "609968029"
        }]
    }
    res.send(data);
});

This is how it is consumed in the other node application

getData = (id) => {
  const options = {
    url: `api/customer/${id}`
  };

  return httpClient.get(options)
    .then(data => {
      const arr = data.arr.map(arrEntry => {
        return {
          myKey: arrEntry.myKey
        };
      });

      return {
        arr
      };
    });
};

Here myKey correctly has the data from the REST API but I'm not sure how my_key is converted to myKey for it work.

Share Improve this question asked Sep 18, 2017 at 8:02 Dinesh PandiyanDinesh Pandiyan 6,3193 gold badges35 silver badges50 bronze badges 2
  • Are there any middleware(s) installed on either end to do processing on the data? What does the data look like on the wire? What Content-Type is it sent with? – T.J. Crowder Commented Sep 18, 2017 at 8:14
  • What is httpClient? – robertklep Commented Sep 18, 2017 at 8:15
Add a ment  | 

2 Answers 2

Reset to default 5

Turns out we have used humps library to resolve the response object from keys snake-case to camelCase.

I found this code in the lib call

const humps = require('humps');
...
axios(optionsObj)
  .then(response => {
    resolve(humps.camelizeKeys(response.data));
  })
  .catch(err => {
    reject(err);
  });

lodash can do this

_.camelCase('Foo Bar');
// => 'fooBar'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745121926a4612464.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信