How to – use synchronous modules on the server
The following examples require Node.js. It will take just a few minutes to install Node.js using the pre-built installer available at https://nodejs.org/download/ or even faster via a package manager at https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager.
We will start by putting a simple logic into a module:
foo.js
console.log( "I'm running" );
Now we can call the module:
main.js
require( "./foo" );
In order to run the example, we will open the console (under Windows, you can simply run CMD.EXE
, but I would recommend an enhanced tool like CMDER available at http://cmder.net/). In the console, we type the following:
node main.js
As soon as Enter is pressed, the console outputs I'm running. So when a module is requested, its body code is invoked. But what if we request the module several times?
main.js
require( "./foo" ); require( "./foo" ); require( "./foo"...