-->

Calling localhost urls on Azure's IISNode

2019-03-02 05:46发布

问题:

I am running into some issues with running a Node app on Azure WebSites due to IISNode. Basically the issue is that I am relying on the port number being a number, which frankly is not the case on Azure ... Here the port number is actually a named pipe, and has values like \\.\pipe\76deda07-ef5c-4539-87d8-755ede772521. Thus, the following piece of code, which routes an internal request back onto the HTTP routes defined on localhost, does not work:

function getFromLocalRoute(restPayloadObject, callback) {

    request.get({
        uri : util.format('http://localhost:%s/api/%s', app.config.port, restPayloadObject.servicepath),
        json : true
    }, callback);
}

It should go without saying that this works fine on *nix systems, but now I need to find a nice way of handling this that works on Windows as well.

What I am after is basically this:

  1. I have a url that exists on localhost
  2. I would like to call the url on localhost using this url
  3. It must work using IISNode

It does not have to use HTTP. I simply want to reuse the logic that is implicit by using Express to handle my routing logic, parsing everything, sending it on to the right modules, etc. So if there is a way of calling the Express Router without using HTTP that would be perfect.

Related:

  • Node HTTP request using a named pipe
  • Is it possible to call Express Router directly from code with a "fake" request?

回答1:

Windows Azure Websites uses IISNode to host the Node process inside of IIS. Your Node site is actually given a Named Pipe which receives the incoming requests, not a TCP port like you would use when running locally or hosting yourself. Even if you could open a TCP port, Azure Websites are really only meant for traditional websites and don't have a way to open ports to the outside world.

As we will get this error message like: Error: connect EACCES 127.0.0.1:80 at Object.exports._errnoException (util.js:837:11) at exports._exceptionWithHostPort (util.js:860:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1060:14)

As a workaround, If your URL which is wanted is in your expressjs project, you can leverage res.redirect or custom build-in function to handle params. Or you can migrate your project to Azure VM which offers greater flexibility for your need.



回答2:

I posted another question that was related to this problem, and I found a decent programmatic solution that applies to this problem.

Turns out it is possible to call express.Router directly by using the (non-public/unofficial) Router#handle method with a stubbed out request and response object. For details on that approach you can refer to this answer.

It turned out that the named pipe approach was non-viable, so calling the Router directly seems like the closest fit for the problem at hand.