bzarras / newsapi

A promise-based node interface for NewsAPI
MIT License
145 stars 39 forks source link

CORS problem #22

Open Vinit-Dantkale opened 4 years ago

Vinit-Dantkale commented 4 years ago

Access to fetch at 'https://newsapi.org/v2/top-headlines?country=in&category=general&sources=&q=&pageSize=20&page=1' from origin 'http://192.168.0.5:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. I had seen CORS error also on the link where I've deployed my app. Could you let me know how to solve this?

AlexandrAtanasov commented 4 years ago

Hello! The same problem has not yet been resolved. You?

Josh-Uvi commented 4 years ago

I had the same issue and fixed it by using a proxy server.

Vinit-Dantkale commented 4 years ago

Okay, actually for me this works when I run it on localhost but fails here. Can you please have a look at the code. Thank You :)

bzarras commented 4 years ago

Thanks for brining this issue to my attention. If anyone has identified the problem, please feel free to submit a PR. I've been busy with other stuff and haven't been able to give this library much love recently

Vinit-Dantkale commented 4 years ago

I've also been busy with my schedule. If anyone identifies the problem, please do tell, so it can be solved. Thank You for looking in 😄

Vinit-Dantkale commented 4 years ago

Has there been any update on this?

DiegoVallejoDev commented 4 years ago

I've made a fork for it

https://github.com/Destructor1702/newsapi

const NewsAPI = require('newsapi');
const newsapi = new NewsAPI('YOUR_API_KEY', { corsProxyUrl: 'https://cors-anywhere.herokuapp.com/' });

based on this thread

https://stackoverflow.com/questions/61951713/problem-with-cors-policy-when-making-a-request-to-https-newsapi-org

DiegoVallejoDev commented 4 years ago

I also added the cors mode headers by default, so above option might not be needed

const reqOptions = { 'mode': 'cors', headers: { 'Access-Control-Allow-Origin': '*' } };
bzarras commented 4 years ago

@Destructor1702 I have reviewed your PR and left a comment. I will merge and test when it is ready!

bzarras commented 4 years ago

I have merged and released @Destructor1702 's PR to NPM as version 2.4.1. Please install the latest version and let me know if this resolves the issue