How to return data from Axios API with Node.js?

Sometimes, we want to return data from Axios API with Node.js

In this article, we’ll look at how to return data from Axios API with Node.js.

How to return data from Axios API with Node.js?

To return data from Axios API with Node.js, we can return the properties from the data property.

For instance, we write

const axiosTest = async (url) => {
  try {
    const {
      data: response
    } = await axios.get(url)
    return response
  } catch (error) {
    console.log(error);
  }
}

to call axios.get with the url string.

It returns a promise and we get the resolve value of the promise with await.

And then we get the data property to get the response body object and we return it after assigning to response.

We use a catch block to catch any errors.

Conclusion

To return data from Axios API with Node.js, we can return the properties from the data property.