Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js proxy requests working to app server but failing to local host

Node.js proxy requests working to app server but failing to local host

Problem

I've inherited a project that involves a administrator app running on node.js talking to a REST-like api that runs on apache. I'm very familiar with [LM]AMP but a total newbie to node.js.

I'm trying to setup the whole envionment on my local machine, which is MAMP.

When I use the original settings (in a node config file under a directory called 'build'), where the app host points to the real Server, the node.js site functions correctly:

var options = underscore.defaults(options, {
    ...
    port: 8000,
    proxy_app: true,
    app_host: 'www.example.com',
    app_port: 80
});

In a browser, going to http://localhost:8000/app/do/something returns some valid json.

(my understanding here is limited, but I believe node is proxying this request over to the apache server on which the app is running).

However, when I point it to my local server...

var options = underscore.defaults(options, {
    ...
    port: 8000,
    proxy_app: true,
    app_host: 'www.mylocalexample.com',
    app_port: 80
});

Now, going to http://localhost:8000/app/do/something returns a 404 Not Found.

So the only difference in this node.js config file is the host, which is valid (if I call the app api directly on my localhost it works, ie I get back valid json). For some reason the request proxied by node.js doesn't seem to get sent to my MAMP server, or does get sent but MAMP is rejecting it. There are no errors in the apache log.

Any ideas? Firebug is also not telling me much of use, so I'm not sure how to debug this further. My instinct says it must be some setting on my MAMP's Apache but I'm not sure what it could be.

Problem courtesy of: PeterG

Solution

Presumably your app is examining the Host header in your HTTP request and routing based on it ("virtual hosting"). Going to localhost... in your browser is going to set that header to localhost and therefore it won't match anything that's looking for www.mylocalexample.com. Try making an entry in your hosts file that points www.mylocalexample.com at 127.0.0.1 and then sticking www.mylocalexample.com into your browser.

Solution courtesy of: ebohlman

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js proxy requests working to app server but failing to local host

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×