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:
However, Node.js has some disadvantages:
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/)
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!
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
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.
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.
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.
23-02-2024 02:02:07
22-02-2024 03:19:32
22-02-2024 03:16:03
22-02-2024 03:14:03
22-02-2024 03:11:58