Home > database >  Why Axios GET request sending twice in Node Express?
Why Axios GET request sending twice in Node Express?

Time:09-28

I wrote a very simple Express app that uses Axios to GET data from an API.

I tested with Google Chrome and was able to get API data successfully. But checking the console log, it seems that the GET request from Axios was ran 2 times. 1 time success, 1 time fail.

I am not sure if this is working as intended or am I doing something wrong. Any help would be appreciated.

Code below

const app = express()

app.get('/:id' , (req, res)=>{

    const fullURL = url   req.params.id
    
    axios.get( fullURL , {
        params: {
            apikey: apikey,
            limit: 10
        }
    }).then((response)=>{
        console.log('Success')
        res.send(response.data)
        
    }).catch((error)=>{
        console.log('Error')
    })
})

app.listen(PORT, ()=>{
    console.log(`App is running on port ${PORT}`);
})

Below is my console log result

Success
Error

CodePudding user response:

Try and print the id in the request. I believe it is the /favicon.ico request that is sent by the browser which is being taken as the id in your api. So, the first request is a proper one with the correct id and it gives you success response. the next request is the favicon.ico request that gets you an error from axios! Cause I observed the same using browser and postman. From browser -> I get 2 requests, From postman -> I get 1 request

CodePudding user response:

Maybe try to print the error object you receive within the catch block. My guess is that you get into the then block, prints 'success', then you probably getting an exception in the send function, and then you reach the catch block and prints 'error'.

  • Related