Frontend Data Fetching

Using Fetch

simple fetch
import fetch from 'node-fetch';
fetch('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY')
  .then((response) => response.json())
  .then((data) => console.log(data));

Using Axios

axios
const axios = require('axios');
 
(async () => {
  try {
    const response = await axios.get(
      'https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY'
    );
    console.log(response.data.url);
    console.log(response.data.explanation);
  } catch (error) {
    console.log(error.response.body);
  }
})();

Using Https

simple fetch
import https from 'https';
 
async function get_page(substr) {
  const url = `https://jsonmock.hackerrank.com/api/movies/search/?Title=${substr}`;
 
  return new Promise((resolve) => {
    https.get(url, (res) => {
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
 
      res.on('end', () => {
        resolve(console.log(data));
      });
    });
  });
}
 
// usage
 
(async () => await get_page('spiderman'))();