Introduction to Node.js Development

18-01-2024 02:53:18

Node.js is an open-source and free runtime environment, suitable for various purposes. It is particularly apt for generating dynamic content and can swiftly establish a blog website. Built on JavaScript, a language familiar to many developers, Node.js eliminates the need to learn a new language. It also provides npm, a package manager accessing the world's largest library of components.

Moreover, Node.js offers these advantages:

  • Native support for asynchronous requests. When a Node.js script receives a user request, it can process new requests without waiting for the current one to complete.
  • Compatibility with most Linux operating systems.
  • Integration of many fundamental functionalities, such as editing and deleting files, connecting to MySQL and MongoDB databases, etc.

However, Node.js has some disadvantages:

  • As a relatively newer language compared to the mature PHP, Node.js experiences rapid updates in its codebase. Consequently, code written for earlier versions may quickly become outdated.
  • For certain functionalities, Node.js may require more coding time due to the absence of some popular libraries. For instance, PHP supports the ImageMagick library for image processing, which Node.js currently does not.
  • Compared to Java, Node.js is more challenging in debugging and exception handling. Locating the exact line of error in a JavaScript file with thousands of lines can be difficult.

Basic Usage of Node.js

Node.js can be installed on Ubuntu, Debian, or CentOS operating systems, requiring at least 256MB of memory. Here, we demonstrate basic Node.js usage using a CentOS cloud server.

To install Node.js, use the following commands:

yum install nodejs -y

After successful installation, check the versions of Node.js and the package manager:

node -v
npm -v

Node.js Official Manual:(https://nodejs.org/en/docs/)

Example 1: Hello, World!

In this example, we develop a basic "Hello, World!" program.

Create a file named HelloWorld.js.

vi HelloWorld.js

The content is as follows:

console.log("Hello, world!"); 

After saving and exiting, run this file.

node HelloWorld.js

The output is as follows:

Hello, world!

Example 2: Variable Calculation

This example covers basic mathematical operations.

Create a file named MathTest.js.

vi MathTest.js

The content is as follows:

var a = 5; 
var b = 10;
var c = "Hello, world!";

console.log(c); 
console.log("a = " + a + ", b = " + b); 
console.log("a + b = " + (a + b));

After saving and exiting, run this file.

node MathTest.js

The output is as follows:

Hello, world!
a = 5, b = 10
a + b = 15

Example 3: Creating a Web Server

This example teaches how to create a Node.js web server.

Create a file named WebTest.js.

vi WebTest.js

The content is as follows:

var http = require("http");
var a = 5, b = 10; 

http.createServer(function (request, response) {
    console.log("Request received!");
    response.writeHead(200, {'Content-Type': 'text/html'}); 
    response.write("<i>Hello, world! a + b = " + (a + b) + "</i>"); 
    response.end(); 
}).listen(8080);

After saving and exiting, run this file.

node WebTest.js

Then access http://IP:8080 (ensure the firewall has opened port 8080), and the browser will display the following:

Hello, world! a + b = 15

Note: We can stop the web server using the Ctrl + C shortcut.

Example 4: Installing Third-Party Component Libraries

The above demonstrates basic Node.js usage. Next, we'll show how to install and use third-party component libraries through npm. We'll extend the "Hello, World!" example and install a third-party component called colo, which can change terminal colors.

To install colo, use the following command:

npm i colo  

After installation, edit the HelloWorld.js file created earlier. The content is as follows:

var colour = require("colo");
console.log(colour.red.bold("Hello, world!"));

After saving and exiting, run this file. You will see "Hello, world!" in red and bold.

Conclusion

We have completed four introductory examples of Node.js. Although simple, these examples showcase the basic concepts and mechanisms of Node.js. With this foundational knowledge and the official Node.js manual, you can create any desired program! Further practice will enhance your proficiency in Node.js.