Node.js Getting Started: Difference between revisions
m Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:JavaScript |
m Text replacement - "mlw-continue" to "code-continue" |
||
Line 55: | Line 55: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The above commands will install relatively old version. | The above commands will install relatively old version. | ||
<syntaxhighlight lang="shell" class=" | <syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
node -v | node -v | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell-session" class=" | <syntaxhighlight lang="shell-session" class="code-continue"> | ||
v10.19.0 | v10.19.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell" class=" | <syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
npm -v | npm -v | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 69: | Line 69: | ||
The version could be updated via the <code>n</code> module from <code>npm</code> as follow ([https://askubuntu.com/a/480642/566421 reference]). | The version could be updated via the <code>n</code> module from <code>npm</code> as follow ([https://askubuntu.com/a/480642/566421 reference]). | ||
<syntaxhighlight lang="shell" class=" | <syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
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 | sudo n stable # sudo n latest | ||
</syntaxhighlight>Then check the version once again.<syntaxhighlight lang="shell" class=" | </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=" | <syntaxhighlight lang="shell-session" class="code-continue"> | ||
v16.14.0 | v16.14.0 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="shell" class=" | <syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
npm -v | npm -v | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 87: | Line 87: | ||
== Test Node.js == | == Test Node.js == | ||
Create a working directory and open this directory inside of Visual Studio Code (VSC).<syntaxhighlight lang="shell" class=" | Create a working directory and open this directory inside of Visual Studio Code (VSC).<syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
mkdir ~/tmp/nodejs-first-app | mkdir ~/tmp/nodejs-first-app | ||
cd ~/tmp/nodejs-first-app | cd ~/tmp/nodejs-first-app | ||
code . | code . | ||
</syntaxhighlight>Inside the directory create the following file.<syntaxhighlight lang="shell" class=" | </syntaxhighlight>Inside the directory create the following file.<syntaxhighlight lang="shell" class="code-continue"> | ||
app.js | app.js | ||
</syntaxhighlight><syntaxhighlight lang="javascript"> | </syntaxhighlight><syntaxhighlight lang="javascript"> | ||
Line 98: | Line 98: | ||
} | } | ||
sayHello("Spas"); | sayHello("Spas"); | ||
</syntaxhighlight>Then go back in the terminal (or open the integrated terminal of VSC by pressing <kbd class="noTypo">Ctrl</kbd>+'''<kbd class="noTypo">`</kbd>''') and run the file by using it as an argument of the <code>node</code> command.<syntaxhighlight lang="shell" class=" | </syntaxhighlight>Then go back in the terminal (or open the integrated terminal of VSC by pressing <kbd class="noTypo">Ctrl</kbd>+'''<kbd class="noTypo">`</kbd>''') and run the file by using it as an argument of the <code>node</code> command.<syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
node app.js | node app.js | ||
</syntaxhighlight><syntaxhighlight lang="shell-session"> | </syntaxhighlight><syntaxhighlight lang="shell-session"> | ||
Line 105: | Line 105: | ||
== Install MySQL at WSL == | == Install MySQL at WSL == | ||
Install <code>mysql-server</code> and <code>mysql-client</code>.<syntaxhighlight lang="shell" class=" | Install <code>mysql-server</code> and <code>mysql-client</code>.<syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
sudo apt install -y mysql-client mysql-server | sudo apt install -y mysql-client mysql-server | ||
sudo service mysql start | sudo service mysql start | ||
Line 112: | Line 112: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Then create a MySQL user that is able to log-in into the MySQL server by using native authentication. Gran to the user all privileges. | Then create a MySQL user that is able to log-in into the MySQL server by using native authentication. Gran to the user all privileges. | ||
<syntaxhighlight lang="shell" class=" | <syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
sudo mysql | sudo mysql | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="mysql" line="1" class=" | <syntaxhighlight lang="mysql" line="1" class="code-continue"> | ||
CREATE USER 'local_admin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'local_admin_password_1!2@3#'; | 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; | GRANT ALL PRIVILEGES ON *.* TO 'local_admin'@'localhost' WITH GRANT OPTION; | ||
SELECT user,plugin,host FROM mysql.user WHERE host = 'localhost'; | SELECT user,plugin,host FROM mysql.user WHERE host = 'localhost'; | ||
</syntaxhighlight><syntaxhighlight lang="text" highlight="5" class=" | </syntaxhighlight><syntaxhighlight lang="text" highlight="5" class="code-continue"> | ||
+------------------+-----------------------+-----------+ | +------------------+-----------------------+-----------+ | ||
| user | plugin | host | | | user | plugin | host | | ||
Line 136: | Line 136: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Now log-in by that user in order to test it and create one new empty data base for the further tests. | Now log-in by that user in order to test it and create one new empty data base for the further tests. | ||
<syntaxhighlight lang="shell" class=" | <syntaxhighlight lang="shell" class="code-continue" line="1"> | ||
mysql -u'local_admin' -p'local_admin_password_1!2@3#' | mysql -u'local_admin' -p'local_admin_password_1!2@3#' | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="mysql" line="1" class=" | <syntaxhighlight lang="mysql" line="1" class="code-continue"> | ||
CREATE DATABASE nodejs_w3s_tests; | CREATE DATABASE nodejs_w3s_tests; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="text" class=" | <syntaxhighlight lang="text" class="code-continue"> | ||
Query OK, 1 row affected (0.01 sec) | Query OK, 1 row affected (0.01 sec) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 149: | Line 149: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
In order to start the MySQL server automatically at user log-in, create or update the file <code>wsl-services.bat</code>, located in the Windows user's folder <code>[https://www.softwareok.com/?seite=faq-Windows-10&faq=151 shell:startup]</code>, with the following entry. | In order to start the MySQL server automatically at user log-in, create or update the file <code>wsl-services.bat</code>, located in the Windows user's folder <code>[https://www.softwareok.com/?seite=faq-Windows-10&faq=151 shell:startup]</code>, with the following entry. | ||
<syntaxhighlight lang="shell" class=" | <syntaxhighlight lang="shell" class="code-continue"> | ||
wsl-services.bat | wsl-services.bat | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 07:29, 26 September 2022
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
Introduction
- Node.js is an open source server environment. Node.js is free.
- Node.js runs on various platforms (Windows, Linux, Unix, Mac OS 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.
Windows. Within Windows (or iOS) you could go to the NodeJs.org and download and install the latest stable or LTS or version.
Ubuntu or WSL. Within Ubuntu or WSL 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
Then check the version once again.
node -v
v16.14.0
npm -v
8.3.1
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
Install MySQL at WSL
Install mysql-server
and mysql-client
.
sudo apt install -y mysql-client mysql-server
sudo service mysql start
sudo service mysql status
mysql --version
Then create a MySQL user that is able to log-in into the MySQL server by using native authentication. Gran to the user all privileges.
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 order to test it and create one new empty data base for the further 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 order to start the MySQL server automatically at user log-in, create or update the file wsl-services.bat
, located in the Windows user's folder shell:startup
, with the following entry.
wsl-services.bat
C:\Windows\System32\wsl.exe -u root /etc/init.d/mysql start
References:
- Open Ubuntu terminal using batch file on windows with command?
- Ubuntu 18.04 on WSL Cron daemon not running after reboot