Node.js Getting Started: Difference between revisions

From WikiMLT
m Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:JavaScript
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
<noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude>
<noinclude><!--[[Category:JavaScript|?]]-->{{ContentArticleHeader/JavaScript}}</noinclude>


== References ==
{{collapse/begin}}
 
* [https://codewithmosh.com/p/the-complete-node-js-course Code with Mosh: The Complete Node.js Course]
* [https://www.w3schools.com/nodejs/default.asp W3School: Node.js Tutorial]
* [https://www.w3schools.com/js/default.asp W3School: JavaScript Tutorial]
* [https://www.w3schools.com/jsref/default.asp W3School: JavaScript Reference]
*[https://nodejs.org/en/ '''NodeJs.org'''] / [https://nodejs.org/en/download/ Downloads]
*[https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions NodeJs,org: Installing Node.js via package manager - Debian and '''Ubuntu based Linux distributions''']
*[https://nodejs.org/en/download/package-manager/#n NodeJs,org: Installing Node.js via package manager - '''n''']
 
== Introduction ==
== Introduction ==
 
{{collapse/div|#What is Node.js?}}
* Node.js is an open source server environment. Node.js is free.
* Node.js is an open source server environment. Node.js is free.
* Node.js runs on various platforms (Windows, Linux, Unix, MacOS X, etc.). Node.js uses JavaScript on the server.
* Node.js runs on various platforms (Windows, Linux, Unix, MacOS X, etc.). Node.js uses JavaScript on the server.
Line 44: Line 35:
** Node.js files must be initiated on the server before having any effect.
** Node.js files must be initiated on the server before having any effect.
** Node.js files have extension <code>.js</code>
** Node.js files have extension <code>.js</code>
{{collapse/end}}


== Installation of Node.js and NPM ==
== Installation of Node.js and NPM ==
Line 73: Line 65:
sudo npm cache clean -f
sudo npm cache clean -f
sudo npm install -g n
sudo npm install -g n
sudo n stable   # sudo n latest
</syntaxhighlight>
</syntaxhighlight>Then check the version once again.<syntaxhighlight lang="shell" class="code-continue" line="1">
<syntaxhighlight lang="shell" class="code-continue mlw-shell-gray" line="1">
sudo n stable
</syntaxhighlight>
<syntaxhighlight lang="shell" class="code-continue" line="1">
sudo n latest
sudo npm install -g npm@latest
</syntaxhighlight>Then check the version once again.
<syntaxhighlight lang="shell" class="code-continue" line="1">
node -v
node -v
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session" class="code-continue">
<syntaxhighlight lang="shell-session" class="code-continue">
v16.14.0
v19.6.0
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell" class="code-continue" line="1">
<syntaxhighlight lang="shell" class="code-continue" line="1">
Line 84: Line 83:
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang="shell-session">
<syntaxhighlight lang="shell-session">
8.3.1
9.4.2
</syntaxhighlight>
</syntaxhighlight>


Line 105: Line 104:
</syntaxhighlight>
</syntaxhighlight>


== See also ==
* [[Install MySQL at WSL|'''Install MySQL at WSL''']]
== References ==
* [https://codewithmosh.com/p/the-complete-node-js-course Code with Mosh: The Complete Node.js Course]
* [https://www.w3schools.com/nodejs/default.asp W3School: Node.js Tutorial]
* [https://www.w3schools.com/js/default.asp W3School: JavaScript Tutorial]
* [https://www.w3schools.com/jsref/default.asp W3School: JavaScript Reference]
*[https://nodejs.org/en/ '''NodeJs.org'''] / [https://nodejs.org/en/download/ Downloads]
*[https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions NodeJs,org: Installing Node.js via package manager - Debian and '''Ubuntu based Linux distributions''']
*[https://nodejs.org/en/download/package-manager/#n NodeJs,org: Installing Node.js via package manager - '''n''']


<noinclude>
<noinclude>
Line 111: Line 123:
  | Прндл  = JavaScript
  | Прндл  = JavaScript
  | Пдрдб  = Б
  | Пдрдб  = Б
  | Стадий = 3
  | Стадий = 6
  | Фаза  = Разработване
  | Фаза  = Утвърждаване
  | Статус = Разутвърден
  | Статус = Утвърден
  | ИдтПт  = Spas
  | ИдтПт  = Spas
  | РзбПт  = {{REVISIONUSER}}
  | РзбПт  = Spas
  | АвтПт  = Spas
  | АвтПт  = Spas
  | УтвПт  = Spas
  | УтвПт  = {{REVISIONUSER}}
  | ИдтДт  = 1.08.2022
  | ИдтДт  = 1.08.2022
  | РзбДт  = {{Today}}
  | РзбДт  = 12.02.2023
  | АвтДт  = 1.08.2022
  | АвтДт  = 12.02.2023
  | УтвДт  = 14.01.2023
  | УтвДт  = {{Today}}
  | ИдтРв  = [[Special:Permalink/29657|29657]]
  | ИдтРв  = [[Special:Permalink/29657|29657]]
  | РзбРв  = {{REVISIONID}}
  | РзбРв  = [[Special:Permalink/32246|32246]]
  | АвтРв  =  
  | АвтРв  = [[Special:Permalink/32247|32247]]
  | РзАРв  = [[Special:Permalink/29661|29661]]
  | РзАРв  = [[Special:Permalink/29661|29661]]
  | УтвРв  =  
  | УтвРв  = {{REVISIONID}}
  | РзУРв  = [[Special:Permalink/32216|32216]]
  | РзУРв  = [[Special:Permalink/32216|32216]]
}}
}}
</div>
</div>
</noinclude>
</noinclude>

Latest revision as of 11:02, 12 February 2023

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