Node.js pass handle of response object handle to c

2019-02-16 18:57发布

I have an http server and a forked child process. I want the parent to receive requests and pass to forked process using worker.send. and the worker should be able to process and send the response back to the requester using the same response object.

I tried sending the response object in the second parameter of worker.send, but it gives the error This handle type can't be sent

var child_process = require('child_process');

var worker = child_process.fork(filename);

http.createServer(function (req, res) {
    worker.send({ 'event': 'start' }, res); // send response object
}).listen(4000);

I checked in the child_process.js file and it says if it doesn't belongs to some of the types, it will throw the error.

I want to know if there are any other options by which I can send the response object to the forked child.

EDIT:

Okay, here's what I found, I just changed the following

// Instead of
// worker.send({ 'event': 'start' }, res);
worker.send({ 'event': 'start' }, res.socket);

And the forked process is able to call write on the handler it gets.

Is it correct? Can I use it this way? or will there be any implications under some blah blah conditions?

1条回答
祖国的老花朵
2楼-- · 2019-02-16 19:40

The worker.send only takes requests coming from a net.createServer function. Try better with 'net' package instead of http. This resolved my issue

查看更多
登录 后发表回答