21Nov

24_s1

Hey all! The first topic of this article is transferring of parameters and the script for Node.js. To show you the principle, we will create a file with a simple code, so add it (server.js):

// node server.js port=3000

var http = require('http');

http.createServer(function(req, res) {

    res.end("The server is running!");
    
}).listen(3000);

and will order it to take a port for launching from a command line to launch it in the following way:

node server.js port=3000

and this port will be used for listen.

To do so, we can add this parameter to the launch configuration in your IDE (add port=3000). In order to get what has been transferred in a command line, you can perfectly use a variable called process.argv.

// node server.js port=3000

var http = require('http');

console.log(process.argv);

http.createServer(function(req, res) {

    res.end("The server is running!");

}).listen(3000);

Launch it. Now you see the outputted parameters. Note that the first place belongs to node itself followed by the file and further by the port. Here you can easily see that the simplest ways to get the parameter value is to look through an array and get out the respective string. There is a more convenient way – to install a module called optimist:

npm install optimist

Use optimist instead of process.argvand we will see the results. To do so, I need to change the syntax because optimist supports either this kind of syntax:

// node server.js —port=3000

or:

// node server.jsport 3000

// node server.js port=3000

var http = require('http');

console.log(require('optimist').argv);

http.createServer(function(req, res) {

    res.end("The server is running!");

}).listen(3000);

Now I get the options through argvLaunch. As we can see, everything’s perfect. The only thing left to do is to use a variable and change the port within listen:

// node server.js port=3000

var http = require('http');

var opts = require('optimist').argv;

http.createServer(function(req, res) {

    res.end("The server is running!");

}).listen(opts.port);

Restart and see how our server is working with the new port.

http://localhost:3000/

It’s perfect!

So, our next step now. Here node is used for launching, but we usually use another module for development – for example, supervisor, to enable it to restart the server automatically. Respectively, to use supervisor and transfer parameters, you need to do launch it as follows:

supervisor -- server.js --port=3000

Note: double hyphen is obligatory. The system won’t work without it. So, let us do respective changes within configurations. The supervisor module must be global. Launch it. Supervisor has reported to us what it’s been doing.

So, we can get the parameters of the command line, first of all, from the array process.argvsecond, from module optimistand third, you can get the settings from the environment variables. In general, most of them are set by the operating system – in particular, the HOME-named environment variable can be found almost anywhere. We can get it as follows:

// supervisor -- server.js --port=3000

console.log(process.env.HOME);

var http = require('http');

var opts = require('optimist').argv;

http.createServer(function(req, res) {

    res.end("The server is running!");

}).listen(opts.port);

So, launch it. And here it outputted a home directory of the current user.

You can use your own environment variables. For example, within the Express framework that we will study a little bit later, a  environment variable named  NODE_ENV–  is used. It stores information on what mode the launch has got. For example, NODE_ENV=development is a development mode or production is a mode of a live server. In the code, we can check: whether it is NODE_ENV = productionwe can apply extra optimization, otherwise, if it is NODE_ENV = development, we can connect additional debugging output.

// supervisor -- server.js --port=3000

console.log(process.env.HOME);

var http = require('http');

var opts = require('optimist').argv;

http.createServer(function(req, res) {

    if (process.env.NODE_ENV == 'production') {
        // optimization
    } else if (process.env.NODE_ENV == 'development') {
            // additional debugging output

        }
    
    res.end("The server is running!");

}).listen(opts.port);

In order to set the  environment variables we will use one of the several ways. The first one is: if the launch is via IDE, you can set up the environment variables  right there. Whether the launch is outside IDE, indication of  environment variables is executed differently, depending of the operating system. For example, it will be the following for Windows:

set NODE_ENV=production

All further commands launched through the terminal will have the value NODE_ENV=productionUnder Unix systems with Bash wrapping the analogue will be the following:

export NODE_ENV=production

Every further launch will inherit the variable. If you want to execute only one launch with this exact value, your  environment variable is equal to the value and a line further:

NODE_ENV=production supervisor -- server.js --port=3000

So, we’ve analyzed three ways to get process parameters in Node.js:

  1. Out of the command line through process.argv.
  2. Out of the command line, but via optimist module that can handle some standard setting types.
  3. Getting parameters out of  environment variables contained in process.env.

The lesson code can be downloaded from here.

node-24_2

The materials for this article were borrowed from the following screencast

We are looking forward to meeting you on our website soshace.com

Leave a Reply