DEV Community

Medam Mahesh
Medam Mahesh

Posted on

1 1

[Need Help] Getting data using Axios does not work for some URLs

I have a simple express app with "index.js package.json" in a folder.
I have installed all the required libraries.

I am not getting why the below code does not work with the Github API URL but works for the jsonplaceholder.typicode.com URL. Can someone help with this ?

index.js

const express = require('express');
const cors = require('cors');
const axios = require('axios');
axios.defaults.withCredentials = true;

const app = express();
app.use(cors());

app.get('/api', (req, res) => {
// const url = https://jsonplaceholder.typicode.com/todos;
const url = https://api.github.com/users/memahesh;
axios.get(url, { headers: { 'Access-Control-Allow-Origin': '*' } }).then(response => {
res.json({data: response.data});
});

});

const PORT = 5000;

app.listen(PORT, () => console.log(Listening on port ${PORT}));

Console Log:

(node:1912) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT 13.233.76.15:443
[0] at TCPConnectWrap.afterConnect as oncomplete

Thanks in advance,

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (4)

Collapse
 
manan30 profile image
Manan Joshi

You might want to pass in options to axios like this

axios.get( <URL>,  { headers: { 'Access-Control-Allow-Origin':  '*'  } } );
Enter fullscreen mode Exit fullscreen mode

If not could you please post your error message?

Collapse
 
memahesh profile image
Medam Mahesh

I have updated the post. There seems to be a ETIMEDOUT error. But, I know the Github API is working fine.

I am working behind a proxy. Is that an issue ?

Collapse
 
manan30 profile image
Manan Joshi

Have you tried using axios-proxy-fix? It is a npm package.

Thread Thread
 
memahesh profile image
Medam Mahesh

No, I will try that.

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay