2017-03-07 10 views
0

Node.jsを使ってgrafanaページにログインする際に基本認証を使用しようとしていますが、以下のようなエラーです。 enter image description hereNode.jsで要求されたリソースに 'Access-Control-Allow-Origin'ヘッダーが存在しません。

'はlocalhost:4000' が 'localhostを:5000は' 私のapp.jsを保持している私のgrafanaページにproxy_passそのnginxのからです(localhostを: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(); 
}); 

は、誰もがこのためのアイデアを持っています。..以下のようなアクセス制御 - 許可 - 起源となどを追加しましたが、まったく動作しませんか...?

は、私は、あなたがcorsをインストールするべきだと思います。..

+0

エラーは、クライアントサイドのコードが 'localhost:8080'のプロキシを介さずに直接' localhost:8080'と通信しようとしていることを示しています。 – robertklep

答えて

2

ありがとうございます。

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

refができます:よりcors より:here

+0

私の質問にお答えいただきありがとうございます! :) um ..私はすでにコルスを使ってみました。 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 are evil"を忘れましたhttps://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ – Alkaline

関連する問題