Node.js - tutorial - the Node.js environment

revision:


first application

A Node.js application consists of the following three important components:

1/ import required modules − use the "require" directive to load Node.js modules,
2/ create server − a server which will listen to client's requests - similar to Apache HTTP server,
3/ read request and return response − the server created in an earlier step will read the HTTP request made by the client, which can be a browser or a console and return the response.

Step 1 - import required module: use the "require" directive to load the "http module" and store the returned HTTP instance into an http variable as follows: var http = require("http");

Step 2 - create server: use the created http instance and call http.createServer() method to create a server instance and then bind it at port 8081 using the "listen" method associated with the server instance. Pass it a function with parameters request and response.

Step 3 - testing request and response: put step 1 and 2 together in a file called main.js and start the HTTP server.


run node.js scripts from the command line

The usual way to run a Node.js program is to run the "node" globally available command and pass the name of the file you want to execute.

example

      node app.js
    

You can also embed this information into a JavaScript file with a "shebang" line. The "shebang" is the first line in the file, and tells the OS which interpreter to use for running the script.

example

      #!/usr/bin/node
    

how to exit from a Node.js program?

There are various ways to terminate a Node.js application.

ctrl-C: when running a program in the console you can close it with ctrl-C.

process.exit(): the "process core module" provides a handy method that allows you to programmatically exit from a node.js program: process.exit().

- when node.js runs this line, the process is immediately forced to terminate. This means that any callback that's pending, any network request still being sent, any filesystem access, or processes writing to "stdout" or "stderr" - all is going to be ungracefully terminated right away.
- by default the exit code is "0", which means success. Different exit codes have different meaning, which you might want to use in your own system to have the program communicate to other programs.

Set the process.exitCode property: when the program will later end, Node.js will return that exit code. A program will gracefully exit when all the processing is done.

example

      process.exitCode = 1
    

how to read environment variables from Node.js

The process core module of Node.js provides the env property, which hosts all the environment variables that were set at the moment the process was started; process does not require a "require", it's automatically available.

example

The below code runs app.js and set USER_ID and USER_KEY.

USER_ID=239482 USER_KEY=foobar node app.js

Here is an example that accesses the USER_ID and USER_KEY environment variables.

      process.env.USER_ID // "239482"
      process.env.USER_KEY // "foobar"
    

If you have multiple environment variables in your node project, you can also create an .env file in the root directory of your project, and then use the dotenv package to load them during runtime.

example

      # .env file
      USER_ID="239482"
      USER_KEY="foobar"
      NODE_ENV="development"
    

in your js file

      require('dotenv').config();

      process.env.USER_ID // "239482"
      process.env.USER_KEY // "foobar"
      process.env.NODE_ENV // "development"