0

I am developing on Ionic Framework 2.0.0-beta.4. My node version is v4.4.7. I am trying to use NodeJS http.

My code:

    var http = require('http');

    var options = {
      host: 'www.google.com',
      port: 80,
      path: '/index.html',
    };

    http.get(options, function(res) {
      console.log("Got response: " + res.statusCode);

      res.on("data", function(chunk) {
        console.log("BODY: " + chunk);
      });
    }).on('error', function(e) {
      console.log("Got error: " + e.message);
    });

The error i am getting is:

Fetch API cannot load http://www.google.com/index.html. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access.

Got error: Failed to fetch

I have tried searching online but the solutions are not working for me. Does anybody have a suggestion?

EDIT : I am running Node.Js on an Android application.

Community
  • 1
  • 1
Huiting
  • 1,368
  • 7
  • 24
  • 2
    That doesn't make sense, that error is browser-specific, it relates to the [Same Origin Policy](http://en.wikipedia.org/wiki/Same_origin_policy) which is only enforced in browsers. I just tried it, and that code does not produce that error in NodeJS v4 or NodeJS v6. – T.J. Crowder Sep 06 '16 at 07:52
  • I got __`302 Moved`__ error.. Not the one you have specified... – Rayon Sep 06 '16 at 07:53
  • `http.get('http://www.google.co.in', function(res) {` works fine... – Rayon Sep 06 '16 at 08:00
  • @Rayon Think i was not clear in my question. I have tried running `node http.js` which will give me the `302 Moved` error. But when running on my android application, it gives a the `no 'Access-Control-Allow-Origin'` error – Huiting Sep 06 '16 at 08:18
  • @Huiting – It is not related to the code you have shared by any means... – Rayon Sep 06 '16 at 08:20

0 Answers0