Node.js Getting Started

From WikiMLT

In­tro­duc­tion

#What is Node.js?
  • Node.js is an open source serv­er en­vi­ron­ment. Node.js is free.
  • Node.js runs on var­i­ous plat­forms (Win­dows, Lin­ux, Unix, Ma­cOS X, etc.). Node.js us­es JavaScript on the serv­er.
  • Node is a run­time en­vi­ron­ment for ex­e­cut­ing JS code.
  • Es­sen­tial­ly, Node is a C++ pro­gram that em­beds Chrome’s v8 en­gine, the fastest JS en­gine in the world.
  • We use Node to build fast and scal­able net­work­ing ap­pli­ca­tions. It’s a per­fect choice for build­ing REST­ful ser­vices.
  • Node ap­pli­ca­tions are sin­gle-thread­ed. That means a sin­gle thread is used to serve all clients.
  • Node ap­pli­ca­tions are asyn­chro­nous or non-block­ing by de­fault. That means when the ap­pli­ca­tion in­volves I/O op­er­a­tions (e.g. ac­cess­ing the file sys­tem or the net­work), the thread doesn’t wait (or block) for the re­sult of the op­er­a­tion. It is re­leased to serve oth­er clients.
    • Here is how PHP or ASP han­dles a file re­quest:
      1. Sends the task to the computer's file sys­tem.
      2. Waits while the file sys­tem opens and reads the file.
      3. Re­turns the con­tent to the client.
      4. Ready to han­dle the next re­quest.
    • Here is how Node.js han­dles a file re­quest:
      1. Sends the task to the computer's file sys­tem.
      2. Ready to han­dle the next re­quest.
      3. When the file sys­tem has opened and read the file, the serv­er re­turns the con­tent to the client.
    • Node.js elim­i­nates the wait­ing, and sim­ply con­tin­ues with the next re­quest.
    • Node.js runs sin­gle-thread­ed, non-block­ing, asyn­chro­nous pro­gram­ming, which is very mem­o­ry ef­fi­cient.
  • This ar­chi­tec­ture makes Node ide­al for build­ing I/​​​O‑intensive ap­pli­ca­tions.
  • You should avoid us­ing Node for CPU-in­ten­sive ap­pli­ca­tions, such as a video en­cod­ing ser­vice. Be­cause while ex­e­cut­ing these op­er­a­tions, oth­er clients have to wait for the sin­gle thread to fin­ish its job and be ready to serve them.
  • In Node, we don’t have brows­er en­vi­ron­ment ob­jects such as win­dow or the doc­u­ment ob­ject. In­stead, we have oth­er ob­jects that are not avail­able in browsers, such as ob­jects for work­ing with the file sys­tem, net­work, op­er­at­ing sys­tem, etc.
  • What Can Node.js Do?
    • Node.js can gen­er­ate dy­nam­ic page con­tent.
    • Node.js can cre­ate, open, read, write, delete, and close files on the serv­er.
    • Node.js can col­lect form da­ta.
    • Node.js can add, delete, mod­i­fy da­ta in your data­base.
  • What is a Node.js File?
    • Node.js files con­tain tasks that will be ex­e­cut­ed on cer­tain events.
    • A typ­i­cal event is some­one try­ing to ac­cess a port on the serv­er.
    • Node.js files must be ini­ti­at­ed on the serv­er be­fore hav­ing any ef­fect.
    • Node.js files have ex­ten­sion .js

In­stal­la­tion of Node.js and NPM

Win­dows. With­in Win­dows (or iOS) you could go to the NodeJs​.org and down­load and in­stall the lat­est sta­ble or LTS or ver­sion.

De­bian based GNU/​​​Linux like Ubun­tu or in WSL. On De­ba­ian you could in­stall Node.js by the fol­low­ing com­mands.

sudo apt update
sudo apt install -y nodejs npm

The above com­mands will in­stall rel­a­tive­ly old ver­sion.

node -v
v10.19.0
npm -v
6.14.4

The ver­sion could be up­dat­ed via the n mod­ule from npm as fol­low (ref­er­ence).

sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo n latest
sudo npm install -g npm@latest

Then check the ver­sion once again.

node -v
v19.6.0
npm -v
9.4.2

Test Node.js

Cre­ate a work­ing di­rec­to­ry and open this di­rec­to­ry in­side of Vi­su­al Stu­dio Code (VSC).

mkdir ~/tmp/nodejs-first-app
cd  ~/tmp/nodejs-first-app
code .

In­side the di­rec­to­ry cre­ate the fol­low­ing file.

app.js
function sayHello(name) {
    console.log("Hello " + name);
}
sayHello("Spas");

Then go back in the ter­mi­nal (or open the in­te­grat­ed ter­mi­nal of VSC by press­ing Ctrl+`) and run the file by us­ing it as an ar­gu­ment of the node com­mand.

node app.js
Hello Spas

See al­so

Ref­er­ences