I’m really a beginner to Node.js and I have some questions.
Firstly, if I run node on an empty .js file, it terminates. If I run node on a .js file with some simple assignments, it also terminates. What I’m asking is, does the node.js event loop only run when an EventEmitter is created?
Secondly, currently, I’m using http to send a SMS using Twilio every time I get an http request. The wrapper that I’m using is https://github.com/sjwalter/node-twilio/tree/master/lib
If I create a new TwilioClient, the event loop runs until I call process.exit. Should I create a new TwilioClient within each http request handler or should I create one at the beginning of the .js file and use it within the handler? Does it make a difference?
Unlike Ruby’s EventMachine, Node’s loop doesn’t need to be manually closed. Node.js is smart enough to know when it’s done processing everything and terminates automatically.
Having that in mind (the http request handler will run forever or close if you want to close it manually by calling process.exit), you only need to create only 1 TwilioClient outside the http request and use it inside the http request (it’s the same situation for database handlers).