Last Updated:

What is node JS

NodeJS is a development environment for JavaScript that you install on your computer. The basis for executing JavaScript commands in NodeJS is the V8 engine, which is also built into the Google Chrome browser and others.

Commands entered in JavaScript are translated by the NodeJS environment into bytecode and then executed by the processor. Due to the fact that only the NodeJS console running on V8 is responsible for executing commands, and not the browser, it is not possible to use the DOM and BOM. Accordingly, the Window object and the accompanying Document, Element, and others are missing, and only the JavaScript kernel is available for programming.

Most often, NodeJS is used for server web programming. The development environment supports SQL and MongoDB databases. To create server applications, special modules http, ftp and others have been created. In general, a large number of modules have been created for the NodeJS platform to simplify web development and create desktop applications in JavaScript.

Modular system

All built-in functionality is divided into separate modules that provide developers with the necessary functionality for certain areas of development. NodeJS has a huge number of modules. You can also create your own. Thanks to this, the worldwide community of developers is constantly supplementing NodeJS with new modules.

A module is a block of JavaScript code that is responsible for a specific functionality. In order to get acquainted with the available modules, you need to go to the documentation site https://nodejs.org/. Here you can also get acquainted with the functionality of all the provided modules.

Download and install NodeJS

The official nodeJS website is located at https://nodejs.org/en/. NodeJS is an open source project, the source code of which can be viewed in the repository on the github.com.

To download and install:

  1. Go to the official website.
  2. Download LTS (recommended) or the latest version (msi file for Windows).
  3. Run the installer and select the installation path.
  4. By default, some components will be installed in the system32 folder on drive C.
  5. To verify the correctness of the installation, open cmd with the default directory "C:\WINDOWS\system32".
  6. Enter the command "node -v" - if NodeJS is installed correctly, then its version will be displayed on the command line screen.

What's included in the development tools

To use NodeJS, you need at least 2 tools: REPL and a text editor.

REPL

By itself, the NodeJS development environment is referred to as REPL (Read Eval Print Loop) - starting Node.exe you can see a simple console that prompts you to enter a command in JavaScript. This console is used to execute JavaScript code, it is also developed on the basis of the V8 engine. Most often, to work with the console, they use the command line of the operating system and connect to the REPL through it.

Text Editor

You can use any text editor: Notepad++, Notepad standard SublimeText, Atom, VSCode and other code editors. The main thing is that the editor can save files in the ".js" format.

Working with REPL

You can start NodeJS (REPL) by going to the folder with the installed program and running the Node.exe executable file. In the window that opens, you can enter JavaScript commands: variables, function loops, declare constructors, create objects, and do whatever the JavaScript engine provides.

It is pointless to access the Window object and its successors because the code executes the console, not the browser. But prototypes of Object, String, Number, Math, Array and others are available here. In order to display information in the console, you need to use the console.log() function, not in any case document.write(). After entering each command, you need to press Enter.

Running rePL with cmd

Cmd gives more functionality to work with REPL. It allows you to manage project directories, load modules and manage other functionalities. RePL itself is only an executive environment.

To start the REPL, you need to open cmd, which displays "C:\WINDOWS\system32". Next to it you need to write "node" and press Enter. On the next line, a ">" sign appears, which indicates that you can enter a JavaScript command. The "node" command starts REPL, then the cmd mode is converted to the REPL format.

Example:

C:\WINDOWS\system32>node //run REPL
>function x(){return 5+5} // write a simple JavaScript
function >console.log(x()); call the result of the function in the console
10 // the result of the function
> // REPL is still waiting for the input of the next command

To run a file with code using cmd

REPL can also execute code files in the ".js" format. To do this, you need to specify this file in cmd. For example, let's create a code.js file and enter the code "function x(){return 5+5} console.log(x());" Save this file to the directory D:\programs\NodeJSModulus

Example:

Open the command prompt and enter
C:\WINDOWS\system32>D: // go to the drive D
:> cd programs // go to the folder "programs"
D:\programs> cd NodeJSModulus //then to the next folder
D:\programs\NodeJSModulus> node code.js // run repl and pass it the code file.js
10 // the result of the function call will be displayed

>

Connecting and using modules

Why should we write incomprehensible code in the console, if there is simply no point in it. NodeJS is most often used to connect modules with ready-made code that expand the capabilities of web developers. For example, the http module gives the programmer the opportunity to turn the computer into a server, and the browser can send requests and receive responses to the desired port. To connect a module, use the require(moduleName) command.

After installing NodeJS, all the main modules are copied to the folder with the files. The programmer can only connect the necessary module. For example, let's create an http.js file, save it to drive D, and write the code from the example below to it.

Example: Connecting the "http" module

The file code http.js
var http_protocol = require(http)); save the module object in the variable
http_protocol.createServer(function(request,response){ // method from the module, which allows the browser to receive requests and send responses

response.end(«Hello World!»);

}).listen(3000, "127.0.0.1", function(){
console.log ("Server started");

});

Run this file in REPL
D:\node http.js
// The message "Server started" will be displayed

Now you need to open the browser and enter localhost:3000 in the search field. On the browser screen you will see the inscription Hello World!

Total

The construct "var x = require(moduleName)" creates a module object whose name is specified in the require() constructor parameter. Further, through the variable, you can refer to the functionality of the module. This can be done both in the file and by entering commands directly into the REPL.