Sajal Shrestha
Sajal Shrestha

Reputation: 554

How to fetch a data from an API in MERN stack web application project?

I am building a library management system using MERN Stack, where the ISBN number is fetched from free Google Book API. But I am confused about how to fetch an API to my application.

Upvotes: 1

Views: 1180

Answers (1)

1565986223
1565986223

Reputation: 6718

You can use fetch API implemented by browsers or AJAX for using with ReactJS (or any frontend code for that matter)

With nodejs you can use pacakages like request, request-promise, node-fetch, axios in similar fashion.

An example using request library available for nodejs

request
  .get('http://google.com/img.png' // api url)
  .on('response', function(response) {
    console.log(response.statusCode) // 200
    console.log(response.headers['content-type']) // 'image/png'
    // save the image somewhere, or render to webpage
  })
  .pipe(request.put('http://yoursite.com/img.png'))

Upvotes: 1

Related Questions