1

So i have successfully set up https server in node on localhost. And it's working, but i have link from third party api, that i want to call and the display the data in front end. When i try to fetch it on front end i get No 'Access-Control-Allow-Origin' header is present on the requested resource. So i think i need to create https proxy for this. Thanks so much.

here is my node.js

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

var a = https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("Nemke");
}).listen(8000);

And the link to the API https://api.kursna-lista.info/b7b80a59415046c33449b6a2a96bd4d8/kursna_lista

Now what I want to achieve is to:

fetch('https://localhost:8000/api.kursna-lista.info/b7b80a59415046c33449b6a2a96bd4d8/kursna_lista').then(res => res.json()).then(data => data).catch(err => err)
NemanjaD
  • 258
  • 1
  • 14
  • 1
    This doesn't make much sense. Why are you trying to fetch data from `localhost`? Wouldn't you just use `fetch('https://api.kursna-lista...`? – Phil Sep 01 '17 at 03:50
  • There is no meaning to access 3rd party URL from your localhost because it's not yours. Within localhost you can access only things those are on your server. Do you want to do `fetch('https://api.kurs...`? – Anshuman Jaiswal Sep 01 '17 at 03:52
  • It sounds like you really want to call an external API and return data from that. Might I suggest using [Express.js](https://expressjs.com/) for the web server portion and using something like [axios](https://www.npmjs.com/package/axios) to make request to other services. Then you can hit a specific endpoint like `localhost:8000/some_external_call` to perform whatever action you want. – stetson Sep 01 '17 at 03:53
  • The problem here is I get No 'Access-Control-Allow-Origin' header is present on the requested resource this error when calling the api link, in front end. – NemanjaD Sep 01 '17 at 04:03
  • If you're running into CORS issues you can modify how you're calling fetch. See [here](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) for more info on using fetch with CORS. Then you can just call fetch directly on your `https://api.kursna-lista....` url. – stetson Sep 01 '17 at 04:12
  • @stetsmando hi there I have used this. var request = new Request('test.json', { method: 'GET', mode: 'cors', redirect: 'follow', headers: new Headers({ 'Content-Type': 'text/plain' }) }); Still didn't work. – NemanjaD Sep 01 '17 at 04:17
  • @NemanjaD where are you getting Request from? Are you doing this on your local server? Or from the front end? – stetson Sep 01 '17 at 04:19
  • @stetsmando yes on localhost in frontend. – NemanjaD Sep 01 '17 at 04:20
  • @NemanjaD If you're using a front end to call a back on the same host you need to enable CORS on each of them. This would be a lot easier if you used a framework like express for your backend. – stetson Sep 01 '17 at 04:27
  • @hi yes I am using express, but how do I call app link in node ? – NemanjaD Sep 01 '17 at 04:36

1 Answers1

0

You will need to create a controller in your back end, which calls the third party api and return the result to front end. And your front end will call the controller instead of calling third party.

However, if you are able to change the source code of this third party application, you can enable cross origin, and that should fix the issue as well.

How to enable cross-origin resource sharing (CORS) in the express.js framework on node.js

Alex
  • 1,692
  • 13
  • 10