Jamie Hutber
Jamie Hutber

Reputation: 28064

The requested URL "[no URL]", is invalid. Node http-proxy

I believe I am missing a fundimental part of the proxy set up here but when using the following:

var http = require('http'),
    httpProxy = require('http-proxy');
      httpProxy.createProxyServer({target:'http://www.asos.com'}).listen(8000); 

http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
    res.end();
}).listen(9000);

To be presented by:

Invalid URL

The requested URL "[no URL]", is invalid.
Reference #9.56731002.1508760714.1524ffde

now I am pretty sure this is a url entered into the proxy?

All I want to do is setup up a proxy to a site and then insert some custom js file. But this is step one.

Upvotes: 1

Views: 4374

Answers (1)

skirtle
skirtle

Reputation: 29092

Contrary to what you've said in the comments you were correct to try to access localhost:8000. That is the correct port for the proxy you created.

You need to add this:

changeOrigin: true

In full:

httpProxy.createProxyServer({
    changeOrigin: true,
    target: 'http://www.asos.com'
}).listen(8000);

Without that setting the remote server will be receiving a request with the header Host: localhost:8000 and it seems that this particular server cares about the Host header (perhaps it's using virtual hosts). As a result it won't know what to do with it and it's returning that error. The proxy is successfully proxying the error message from the remote server.

You've clearly copied your code from the http-proxy documentation but you seem to have misunderstood it. Notice that in the original example the proxy target is localhost:9000, which is the same server it subsequently creates. So the intention of that example is that you will access localhost:8000 and it will proxy the request to localhost:9000. What you've trying to do is quite different. Your code is creating two completely independent servers, one on port 8000 and one on port 9000.

Rather than using the listen method you might be better off looking at the examples for the web method.

Upvotes: 3

Related Questions