Node.js Getting Started
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, 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
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