Home > Net >  Axios in node.js check url before executing code
Axios in node.js check url before executing code

Time:01-29

I dont find anything that works for me so I ask here:

I am trying to get some data from a website with axios. It works great, but if I input the wrong URL I get an error. I want to check if the URL with its complete path to the page I desire exists.

I tried npm package url-exists but it just said the path didn't exist eventho it did.

Is there a simple way to check if a path like for example "https://github.com/enterprise" exists or if it doesn't?

CodePudding user response:

You have to make a request anyway to check if the url exists. So you can handle it when you got 404 status code in catch error block of axios function.

CodePudding user response:

The only way is making the request, you can handle this scenario using a try/catch block

const getSomething = async () => {
  try {
    const something = await axios.get("www.website.fake")
  } catch (err) {
    throw new Error(err)
  }
}

CodePudding user response:

Then you need to check your source code.

  • Related