-1

I'm trying to use the g-trends API in my vuejs application running from localhost, but keep running into issues of my requests getting blocked due to CORS restrictions: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I know how to fix the issue when performing a fetch request, but not in a scenario where I don't actually see the request URL like when using an API. Is there a way to fix this?

const { ExploreTrendRequest } = require('g-trends');
const explorer = new ExploreTrendRequest();
explorer.past5Years()
        .addKeyword("keyword")
        .download().then(csv => {
          console.log(csv)                         
        })
thesep
  • 9
  • 1
  • 1
    That package isn't supposed to be used on the client-side, I guess. Let your backend make the requests and forward them to your Vue app. (and inserting cors-anywhere into a URL isn't a fix ;) –  May 02 '20 at 11:13
  • Unfortunately I don't have access to the backend of that project :/ – thesep May 02 '20 at 13:33
  • @ChrisG Actually `g-trends` is a client side module. https://www.npmjs.com/package/g-trends –  May 02 '20 at 16:05
  • @CiBoz How do you figure? I've looked at that link but don't see anything that says "client-side". –  May 03 '20 at 09:28
  • It states `G Trends client is designed to be ...` Client means client-side. Or am i wrong? –  May 03 '20 at 09:30
  • Not necessarily, but when you make a request to the Google Trends API from a node program, then your node program acts as client to Google's server. `npm install` and `require()` also suggest node, and so does OP's entire question, because when used from node there won't be any CORS issues. –  May 03 '20 at 09:36

1 Answers1

0

I usually use a proxy server for avoiding this situation, your app asks your server to do that request for you so the communication with the API is managed by your server, when your server receives the answer it forwards it to your app and there won´t be any CORS problem.