1

I am currently using GrapheneDB for my Neo4J database which I am pulling information from my Ember application. Neo4J requires Basic HTTP Authentication and since I want a more secure method (instead of explicityly stating the headers in my ajax calls), I am trying to use a http-proxy to connect to the database. So via Ember-CLI, I generated a http-proxy with a path at '/api'. Within the proxy file, I have the following:

~/server/proxies/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/' });
  });
};

So when the above is ran, the headers when printed out on the server seems correct:

{ 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>' }

But when going to the my api URL, I get the following request headers:

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

And I get a 404 Not Found. But when I remove the authorization header, I get a 401 Unauthorized prompting me for a username/password. Sometimes on the headers it will show: "Authorization: Basic Og==". Either way it doesn't work.

Does anyone know a solution to this? I tried both the example setHeader code in the node-http-proxy documentation and also searched all over the interwebs for information on ember http-proxy but to no avail. Thanks in advanced!

kaiwah.ng
  • 267
  • 2
  • 15

2 Answers2

2

I'm just spit balling here because I can't test this out but I think you have it working. You're getting a 404 because you're requesting 'http://app-id.sb02.stations.graphenedb.com:24789/db/data/api' which probably doesn't exist. It may also be because of the accept header. If you're trying it out in the browser the accept header is text/html but your target may be expecting application/json in which case it 404s. You could try curling it to test this out 'curl -H "ContentType:application/json" [target]'.

GOULETGOULET
  • 451
  • 3
  • 6
  • Thanks for the response GOULETGOULET, it seems you are right. Instead of doing a CURL, I quickly checked it by removing the headers and then logging in manually when the website prompts. And sure enough it 404's after logging in. Does the http-proxy always prepend the target URL to have your prefix "/api" with it? Because Neo4J comes packaged with a browser which you can access directly which would be http://app-id.sb02.stations.graphenedb.com:24789/browser, but changing to this target URL also 404's. Is there something fundamentally wrong with my setup? – kaiwah.ng Sep 26 '14 at 17:30
  • Got it working! It was the appended /api you were mentioning. I had no clue that was added into the url. Thanks a lot! – kaiwah.ng Sep 26 '14 at 17:55
0

As GOULETGOULET stated, '/api' was appended to the end of the target URL and no such directory exists in my target. So swapping it to 'db/data' worked.

kaiwah.ng
  • 267
  • 2
  • 15