简体   繁体   English

Node.js中的请求资源上没有“ Access-Control-Allow-Origin”标头

[英]No 'Access-Control-Allow-Origin' header is present on the requested resource in Node.js

I'm trying to use basic auth to login to my grafana page using Node.js and express, but it shows me an error like below. 我正在尝试使用基本身份验证使用Node.js登录到我的grafana页面并进行表达,但是它向我显示了如下错误。 在此处输入图片说明

The 'localhost:4000' is holding my app.js and 'localhost:5000' is from nginx that proxy_pass to my grafana page(localhost:8080) 'localhost:4000'持有我的app.js,'localhost:5000'来自nginx,该proxy_pass到我的grafana页面(localhost:8080)

Here is my basic auth code 这是我的基本身份验证代码

app.get('/grafana', isLoggedIn, function(req, res, next){
  console.log('Accessing to grafana');
  var auth = "Basic " + new Buffer('admin' + ":" + 'admin').toString("base64");
  request({
    url: 'http://localhost:5000',
    headers:{
      "Authorization": auth
    }             //passing through here
  }, function(err, resp, body){

what is my problem here..? 我的问题在这里..? I added the Access-Control-Allow-Origin and etc like below, but doesn't work at all.. 我像下面一样添加了Access-Control-Allow-Origin等,但是根本不起作用。

app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', "*");
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.header('Access-Control-Allow-Headers', 'Origin, Basic, X-Requested-With, Content-Type, Accept, Authorization');
  res.header('Access-Control-Allow-Credentials', 'true');
  next();
});

Does anybody have an idea for this...? 有人对此有想法吗?

Thank you.. 谢谢..

i think, you should install cors . 我认为,您应该安装cors

npm install cors

Simple Usage : 简单用法:

var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());


app.use(function (req, res, next) {

        // Website you wish to allow to connect
        res.setHeader('Access-Control-Allow-Origin', '*');

        // Request methods you wish to allow
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

        // Request headers you wish to allow
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

        // Set to true if you need the website to include cookies in the requests sent
        // to the API (e.g. in case you use sessions)
        res.setHeader('Access-Control-Allow-Credentials', true);

        // Pass to next layer of middleware
        next();
    });

Can ref : cors more than : here 可以引用cors大于: 这里

This looks similar to this question about using an Apache proxy for Grafana . 这看起来类似于关于为Grafana使用Apache代理的问题

There is an example for nginx in the docs here: 这里的文档中有一个有关nginx的示例:

http://docs.grafana.org/v1.9/installation/#graphite-server-config http://docs.grafana.org/v1.9/installation/#graphite-server-config

auth_basic            "Restricted";
auth_basic_user_file  /path/to/my/htpasswd/file;

if ($http_origin ~* (https?://[^/]*\.somedomain\.com(:[0-9]+)?)) {  #Test if request is from allowed domain, you can use multiple if
    set $cors "true";                                               #statements to allow multiple domains, simply setting $cors to true in each one.
}

if ($cors = 'true') {
    add_header  Access-Control-Allow-Origin $http_origin;           #this mirrors back whatever domain the request came from as authorized, as
    add_header  "Access-Control-Allow-Credentials" "true";          #as long as it matches one of your if statements
    add_header  "Access-Control-Allow-Methods" "GET, OPTIONS";
    add_header  "Access-Control-Allow-Headers" "Authorization, origin, accept";
}

How is your nginx proxy configured? 您的nginx代理如何配置?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js中node-static中请求的资源上没有“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource in node-static in Node.js Node.JS/Fetch:请求的资源上不存在“Access-Control-Allow-Origin”header - Node.JS/Fetch: No 'Access-Control-Allow-Origin' header is present on the requested resource 无法在NODE.JS中解决此问题,请求的资源在(nodejs + angularjs)中不存在“ Access-Control-Allow-Origin”标头 - Unable to solve this in NODE.JS, No 'Access-Control-Allow-Origin' header is present on the requested resource in (nodejs + angularjs) Node.js:请求的资源上不存在“Access-Control-Allow-Origin”header - Node.js: No 'Access-Control-Allow-Origin' header is present on the requested resource Node js Heroku 请求的资源上不存在“Access-Control-Allow-Origin”标头 - Node js Heroku No 'Access-Control-Allow-Origin' header is present on the requested resource Node.Js 错误“请求中不存在‘Access-Control-Allow-Origin’标头” - Node.Js error “No 'Access-Control-Allow-Origin' header is present on the requested” 角度:请求的资源上不存在“Access-Control-Allow-Origin”标头 - ANGULAR: No 'Access-Control-Allow-Origin' header is present on the requested resource Access-Control-Allow-Origin'标头出现在请求的资源上 - Access-Control-Allow-Origin' header is present on the requested resource NodeJ在所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - NodeJs No 'Access-Control-Allow-Origin' header is present on the requested resource Angular 6 - 请求的资源上不存在“Access-Control-Allow-Origin”标头 - Angular 6 - No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM