简体   繁体   中英

Pass variable into a running node.js program

I have an process which is running on my server in the background. The object is exported like this

module.exports = new Application()

because I only want one instance of the object to ever exist.

I then have a separate process which tries to call a prototyped function process of Application . It looks something like this

var app = require('./Application.js');

app.process(process.argv[2]);

and say the file is called process.js . I'll then call node process.js thing_to_pass

The trouble is, every time I do this is it re-instantiates application which leads to behavior I am not looking for.

How can I get around this? I was thinking of using socket.io to communicate between the two processes but this seems hackish.

You aren't going to be able to go down the road you are trying to go down. Because you have two different processes, you are going to make two different instances of Application . Sending messages from one process to another via socket.io isn't that hackish imo, definitely something to consider.

You could also consider using child_process and sending messages with kill http://nodejs.org/api/process.html

如果您使用的是Linux,则建议使用UNIX域套接字。

If you want a running process to receive new data, it has to listen for that data in some way. Whether you set up an HTTP server and accept and process requests with new data, or socket.io, or you wait for info from the command line, watch a file for changes, or what have you.

If you start up a whole separate process, it will be logically segregated in every way from the currently running process, and thank goodness or else completely separate apps could mess with each other in unintended ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM