Ember-CLI - 在HTTP代理上设置HTTP授权标头

时间:2014-09-25 18:30:04

标签: ember.js neo4j ember-cli http-proxy node-http-proxy

我目前正在使用GrapheneDB作为我的Neo4J数据库,我从我的Ember应用程序中提取信息。 Neo4J需要基本的HTTP身份验证,因为我想要一个更安全的方法(而不是明确地说明我的ajax调用中的头文件),我试图使用http代理连接到数据库。所以通过Ember-CLI,我生成了一个http代理,路径为'/ api'。在代理文件中,我有以下内容:

〜/服务器/代理/ api.js

var proxyPath = '/api';

module.exports = function(app) {
  // For options, see:
  // https://github.com/nodejitsu/node-http-proxy
  var proxy = require('http-proxy').createProxyServer({});
  var path = require('path');


  app.use(proxyPath, function(req, res, next){
    var credentials = new Buffer('app-id:app-pw').toString('base64');
    // include root path in proxied request
    req.url = path.join(proxyPath, req.url);
    req.headers.authorization = "Basic " + credentials;

    proxy.web(req, res, { target: 'http://app-id.sb02.stations.graphenedb.com:24789/db/data/' });
  });
};

因此,当运行上述内容时,在服务器上打印出来的标题似乎是正确的:

{ host: 'localhost:4200',
  connection: 'keep-alive',
  'cache-control': 'max-age=0',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8',
  authorization: 'Basic <correct base64 hash>' }

但是当转到我的api网址时,我会收到以下请求标头:

GET /api HTTP/1.1
Host: localhost:4200
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

我得到404 Not Found。但是当我删除授权标题时,我收到401 Unauthorized提示我输入用户名/密码。有时在标题上会显示:“授权:基本Og ==”。无论哪种方式它都不起作用。

有谁知道这个解决方案?我在node-http-proxy文档中尝试了两个示例setHeader代码,并在整个interwebs中搜索有关ember http-proxy的信息,但无济于事。先谢谢!

2 个答案:

答案 0 :(得分:2)

我只是在这里吐痰,因为我无法测试它,但我认为你有它的工作。你得到的是404,因为你要求的“http://app-id.sb02.stations.graphenedb.com:24789/db/data/api”可能不存在。它也可能是因为接受标题。如果你在浏览器中尝试它,接受标题是text / html,但你的目标可能是期望application / json,在这种情况下它是404s。您可以尝试卷曲它来测试'curl -H“ContentType:application / json”[target]'。

答案 1 :(得分:0)

正如GOULETGOULET所说,'/ api'被附加到目标URL的末尾,并且我的目标中不存在这样的目录。所以将它交换为'db / data'就可以了。