2017-03-07 64 views
0

我正在嘗試使用基本身份驗證來使用Node.js登錄到我的grafana頁面並進行表達,但它顯示了我像下面的錯誤。 enter image description hereNode.js中請求的資源上沒有'Access-Control-Allow-Origin'標頭存在

的 '本地主機:4000' 抱着我app.js和 '本地主機:5000' 是由nginx的是proxy_pass我grafana頁(本地主機:8080)

這裏是我的基本身份驗證碼

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){ 

這是什麼問題?我添加了訪問控制允許來源和等像下面,但不能在所有的工作..

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(); 
}); 

有誰有這個想法...?

謝謝。

+0

錯誤指出客戶端代碼試圖直接與localhost:8080通信,而不是通過'localhost:5000'上的代理。 – robertklep

回答

2

我想,你應該安裝cors

npm install cors 

簡單的用法:

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(); 
    }); 

可以參考:cors 超過:here

+0

感謝您回答我的問題! :)嗯..我嘗試過使用cors,如 var cors = require('cors'); var corsOption = {origin:'http:// localhost:4000'}; 我把app.use(cors(corsOption));裏面的功能(err,resp,body) – paulc1111

+0

然而,它不工作...不知道爲什麼.. – paulc1111

+0

@ paulc1111.你可以再次檢查我的答案。我確實增加了一些代碼... – NguyenTungs

0

這類似於這樣question about using an Apache proxy for Grafana

有一個在這裏的文檔的nginx的一個例子:

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"; 
} 

您是如何設定的nginx代理?

+0

你忘了「ifs is evil」https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ – Alkaline

相關問題