Problems with the headings of reply node.js



  • Hello, everyone. I'm launching a simple server on node.js, containing 1 statistic html file from which I'm sending a request to the outside server and receiving a mistake in response to the mistake.

    No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://127.0.0.1:8888 ' is therefore not allowed access

    'Cause there's no matching headline, which is weird, 'cause in the code, I wrote everything right.

    The code I'm launching:

    var path = require('path');
    var express = require('express');
    var app = express();
    

    app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', 'localhost');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
    });

    app.use(express.static(path.join(__dirname, 'content')));

    app.get("/target", function(req, res){
    res.sendFile(__dirname+ '/content/index.html');
    });

    app.listen(8888);
    console.log("Server is listen on 8888 port")

    введите сюда описание изображения

    What's the problem?



  • You have a problem with the host that's in the title:

    res.setHeader('Access-Control-Allow-Origin', 'localhost');
    

    There's got to be this:

    res.setHeader('Access-Control-Allow-Origin', '*');
    

    It's if you want all the requests from the interior to pass. If you don't want to, just write a list of addresses.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2