Node.js Getting Started: Difference between revisions
m (Стадий: 4 [Фаза:Авторизиране, Статус:Разработен]; Категория:JavaScript) |
m (Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:JavaScript) |
||
(One intermediate revision by the same user not shown) | |||
Line 123: | Line 123: | ||
| Прндл = JavaScript | | Прндл = JavaScript | ||
| Пдрдб = Б | | Пдрдб = Б | ||
| Стадий = | | Стадий = 6 | ||
| Фаза = | | Фаза = Утвърждаване | ||
| Статус = | | Статус = Утвърден | ||
| ИдтПт = Spas | | ИдтПт = Spas | ||
| РзбПт = Spas | | РзбПт = Spas | ||
| АвтПт = | | АвтПт = Spas | ||
| УтвПт = | | УтвПт = {{REVISIONUSER}} | ||
| ИдтДт = 1.08.2022 | | ИдтДт = 1.08.2022 | ||
| РзбДт = 12.02.2023 | | РзбДт = 12.02.2023 | ||
| АвтДт = | | АвтДт = 12.02.2023 | ||
| УтвДт = | | УтвДт = {{Today}} | ||
| ИдтРв = [[Special:Permalink/29657|29657]] | | ИдтРв = [[Special:Permalink/29657|29657]] | ||
| РзбРв = [[Special:Permalink/32246|32246]] | | РзбРв = [[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
Introduction
- 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 is a runtime environment for executing JS code.
- Essentially, Node is a C++ program that embeds Chrome’s v8 engine, the fastest JS engine in the world.
- We use Node to build fast and scalable networking applications. It’s a perfect choice for building RESTful services.
- Node applications are single-threaded. That means a single thread is used to serve all clients.
- Node applications are asynchronous or non-blocking by default. That means when the application involves I/O operations (e.g. accessing the file system or the network), the thread doesn’t wait (or block) for the result of the operation. It is released to serve other clients.
- Here is how PHP or ASP handles a file request:
- Sends the task to the computer's file system.
- Waits while the file system opens and reads the file.
- Returns the content to the client.
- Ready to handle the next request.
- Here is how Node.js handles a file request:
- Sends the task to the computer's file system.
- Ready to handle the next request.
- When the file system has opened and read the file, the server returns the content to the client.
- Node.js eliminates the waiting, and simply continues with the next request.
- Node.js runs single-threaded, non-blocking, asynchronous programming, which is very memory efficient.
- Here is how PHP or ASP handles a file request:
- This architecture makes Node ideal for building I/O‑intensive applications.
- You should avoid using Node for CPU-intensive applications, such as a video encoding service. Because while executing these operations, other clients have to wait for the single thread to finish its job and be ready to serve them.
- In Node, we don’t have browser environment objects such as window or the document object. Instead, we have other objects that are not available in browsers, such as objects for working with the file system, network, operating system, etc.
- What Can Node.js Do?
- Node.js can generate dynamic page content.
- Node.js can create, open, read, write, delete, and close files on the server.
- Node.js can collect form data.
- Node.js can add, delete, modify data in your database.
- What is a Node.js File?
- Node.js files contain tasks that will be executed on certain events.
- A typical event is someone trying to access a port on the server.
- Node.js files must be initiated on the server before having any effect.
- Node.js files have extension
.js
Installation of Node.js and NPM
npm
stands for Node Package manager. There are also other package managers available.
Windows. Within Windows (or iOS) you could go to the NodeJs.org and download and install the latest stable or LTS or version.
Debian based GNU/Linux like Ubuntu or in WSL. On Debaian you could install Node.js by the following commands.
sudo apt update
sudo apt install -y nodejs npm
The above commands will install relatively old version.
node -v
v10.19.0
npm -v
6.14.4
The version could be updated via the n
module from npm
as follow (reference).
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 version once again.
node -v
v19.6.0
npm -v
9.4.2
Test Node.js
Create a working directory and open this directory inside of Visual Studio Code (VSC).
mkdir ~/tmp/nodejs-first-app
cd ~/tmp/nodejs-first-app
code .
Inside the directory create the following file.
app.js
function sayHello(name) {
console.log("Hello " + name);
}
sayHello("Spas");
Then go back in the terminal (or open the integrated terminal of VSC by pressing Ctrl+`) and run the file by using it as an argument of the node
command.
node app.js
Hello Spas
See also
References
- Code with Mosh: The Complete Node.js Course
- W3School: Node.js Tutorial
- W3School: JavaScript Tutorial
- W3School: JavaScript Reference
- NodeJs.org / Downloads
- NodeJs,org: Installing Node.js via package manager – Debian and Ubuntu based Linux distributions
- NodeJs,org: Installing Node.js via package manager – n