-->

Node HTTP request using a named pipe

2019-08-10 06:22发布

问题:

I am trying to request a GET through an existing named pipe. This is to work around the fact that Node processes on Azure are wrapped by IISNode, thus not having a port of their own, but rather being given a named pipe (as the PORT environment variable). Node's net.Server class knows how to handle named pipes, which explains why HTTP routing works fine (as http.Server` seems to use the same interface). From the docs:

Class: net.Server on server.listen(path[, callback])

On Windows, the local domain is implemented using a named pipe. The path must refer to an entry in \?\pipe\ or \.\pipe. Any characters are permitted, but the latter may do some processing of pipe names, such as resolving .. sequences. Despite appearances, the pipe name space is flat. Pipes will not persist, they are removed when the last reference to them is closed. Do not forget JavaScript string escaping requires paths to be specified with double-backslashes, such as:

But this is on the receiving/listening end. What I would like to do is to re-use this existing named-pipe to send requests to the listening server, bypassing the external complications of IISNode. Is this even possible? (The named-pipes package does not seem to apply here, as it seems to provide a too high-level interface that does not resemble the low-level socket/EventEmitter nature I am looking for). There are indications that it might not be possible, but that seems to concern explicitly creating named pipes, not reusing existing ones, which is what I want to do. And it does not say will not work, only that it is not supported.

I tried doing this to send a request, but I am failing to get a response. It simply hangs doing nothing.

var namedPipeLocalDomain= app.config.port;

var options = {
    hostname: namedPipeLocalDomain,
    path: util.format('/api/%s', restPayloadObject.servicepath),
    method: 'GET'
};

logger.info('Creating connection using named pipe, ', namedPipeLocalDomain);

var req = http.request(options, function(res) {
    logger.info('STATUS: ' + res.statusCode);
    logger.info('HEADERS: ' + JSON.stringify(res.headers));

    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        body += data
        logger.info('BODY: ' + chunk);
    });
    res.on('end', function() {
        console.log('No more data in response.')
        console.log(body);
    })
});

Some .NET guys I talked were of the impression that this won't work as they thought (not definitive info) a named pipe only accepts one client reading/writing to the pipe.

Related: Calling localhost urls on Azure's IISNode

回答1:

Can you try using process.env.PORT instead of app.config.port and see if it works. If it does not, the only option is to use Azure VM.