I'm working on something, using NodeJS, that's intended to run as a service that I can connect with.
Let's say I'm working on a Calculator npm module.
I will need to run it within my repo as following:
./node_modules/.bin/calculator start
And I want to keep it running forever , and i can connect it somehow (on port maybe?)
So, I can send/receive messages with the calculator using another node module, let's say 'calculator-connector', for example as following:
var calcConnector = require('calculator-connector'),
calc = calcConnector.connect();
calc.add(1, 2);
Any idea how can achieve this design?
I'd make it like this:
Calculator by itself shouldn't be opinionated, where and when it will run. I'd just create it in the moment I need it:
var calculator = require('calculator');
calculator.listen('localhost', 8000); // create the service listening on port 8000
// create client capable of submitting the tasks
calcClient = createCalculatorClient('localhost', 8000)
calcClient.add(1,2)
I believe that such setup is optimal for quick development and debugging.
When you'll need the things to be really separated (say, calculator itself will run on a separate server), you can do simple node
script which'll run the calculator (basically, it's first 2 lines of the snippet above) and then create simple upstart job (in case of debianish server, or some alternative on other platforms), that'll keep the script alive.
PS: check out how express
works, it's beautifully designed: http://expressjs.com/
read more about upstart: http://upstart.ubuntu.com/getting-started.html
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.