2

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.

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? Sep 18, 2017 at 8:14
  • What is httpClient?
    – robertklep
    Sep 18, 2017 at 8:15

2 Answers 2

3

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);
  });
1

lodash can do this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.