how to fetch a website with axios in javascript

To fetch a website with axios in JavaScript, you can make an HTTP request to the website's URL using the axios library. Here is an example:

index.tsx
const axios = require('axios');

axios.get('https://example.com')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.log(error);
  });
175 chars
10 lines

In this example, we use the get method of the axios library to make a GET HTTP request to the URL https://example.com. When the response is returned, we log the response data to the console. If there is an error, we catch the error and log it.

You can also use async/await to fetch the website in a more concise way:

index.tsx
const axios = require('axios');

async function fetchWebsite() {
  try {
    const response = await axios.get('https://example.com');
    console.log(response.data);
  } catch (error) {
    console.log(error);
  }
}

fetchWebsite();
233 chars
13 lines

In the fetchWebsite function, we use await to wait for the response from the GET request to https://example.com. If the request is successful, we log the response data to the console. If there is an error, we catch it and log it.

gistlibby LogSnag