Node.js Getting Started

From WikiMLT

Ref­er­ences

In­tro­duc­tion

  • 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

Then check the ver­sion once again.

node -v
v16.14.0
npm -v
8.3.1

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

In­stall MySQL at WSL

In­stall mysql-serv­er and mysql-client.

sudo apt install -y mysql-client mysql-server
sudo service mysql start
sudo service mysql status
mysql --version

Then cre­ate a MySQL user that is able to log-in in­to the MySQL serv­er by us­ing na­tive au­then­ti­ca­tion. Gran to the user all priv­i­leges.

sudo mysql
CREATE USER 'local_admin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'local_admin_password_1!2@3#';
GRANT ALL PRIVILEGES ON *.* TO 'local_admin'@'localhost' WITH GRANT OPTION;
SELECT user,plugin,host FROM mysql.user WHERE host = 'localhost';
+------------------+-----------------------+-----------+
| user             | plugin                | host      |
+------------------+-----------------------+-----------+
| debian-sys-maint | caching_sha2_password | localhost |
| local_admin      | mysql_native_password | localhost |
| mysql.infoschema | caching_sha2_password | localhost |
| mysql.session    | caching_sha2_password | localhost |
| mysql.sys        | caching_sha2_password | localhost |
| root             | auth_socket           | localhost |
+------------------+-----------------------+-----------+
6 rows in set (0.00 sec)
quit

Now log-in by that user in or­der to test it and cre­ate one new emp­ty da­ta base for the fur­ther tests.

mysql -u'local_admin' -p'local_admin_password_1!2@3#'
CREATE DATABASE nodejs_w3s_tests;
Query OK, 1 row affected (0.01 sec)
quit

In or­der to start the MySQL serv­er au­to­mat­i­cal­ly at user log-in, cre­ate or up­date the file wsl-services.bat, lo­cat­ed in the Win­dows user's fold­er shell:startup, with the fol­low­ing en­try.

wsl-services.bat
C:\Windows\System32\wsl.exe -u root /etc/init.d/mysql start

Ref­er­ences: