DEV Community

Medam Mahesh
Medam Mahesh

Posted on

[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,

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.