Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

node.js - How to fork a child process that listens on a different debug port than the parent

I am trying to use child_process.fork to spawn a process that breaks and listens on the V8 debug protocol.

However, I can't get the forked process to listen on a port that's different from the parent process. Assuming the parent process listens on 6000, the child process also attempts to listen on port 6000:

Failed to open socket on port 6000, waiting 1000 ms before retrying

Here's my code:

// `test.js`, invoked as `node --debug-brk=6000 test.js`

var nodeModule, args, env, child

nodeModule = path.normalize(path.join(__dirname, '..', 'app.js'))

args = [
    '--debug-brk=6001'
  , '127.0.0.1'
  , 3030
  , 'api-testing'
]

env = { 'DB_URI': 'mongodb://localhost/test' }

child = require('child_process')
  .fork(nodeModule, args, {env: env})
  .on('message', callback)

As you can see, I'm trying to get the forked process to listen on port 6001, but the child process attempts to listen on port 6000 which is in use by the parent.

How can I get the child process to listen on port 6001, or some other free port?

There are several threads on this subject. For example:

However:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simple enough answer, found on this comment and with some help from #Node.js on Freenode:

Just move the --debug-brk into the execArgv key of the options param to fork:

// Excerpt:

args = [
   '127.0.0.1'
  , 3030
  , 'api-testing'
]

env = { 'DB_URI': 'mongodb://localhost/test' }

child = fork(nodeModule, args, {
    env: env
  , execArgv: ['--debug-brk=6001']
})
  .on('message', this.callback)

execArgv is the array of parameters passed to the node process. argv is the set passed to the main module. There's a dedicated parameter to child_process.fork for argv, but execArgvs have to be placed within the opts param. This works, and in the child process we have:

> process.execArgv 
["--debug-brk=6001"]
> process.argv
["/usr/local/Cellar/node/0.10.13/bin/node" "/Users/dmitry/dev/linksmotif/app.js", "127.0.0.1", "3030", "api-testing"] 

In summary

Node.js consistently treats execArgv and argv as separate sets of values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...