Node.js is a JavaScript runtime environment that runs on the Chrome V8 JavaScript engine. You can use Node.js to build scalable network applications. This topic describes how to install Node.js and deploy a test project on a Linux Elastic Compute Service (ECS) instance.
Background Information
Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js is ideal for data-intensive real-time applications that run on distributed devices. The Node.js package manager (npm) is an ecosystem of open-source libraries. Node.js is suitable for the following scenarios:
Real-time applications: instant messaging and real-time push notification applications, such as Socket.IO.
Distributed applications: applications that perform efficient parallel I/O that consumes existing data.
Utilities: a variety of utilities from frontend compression and deployment applications such as grunt to graphical desktop applications.
Game applications: real-time and high-concurrency applications in the gaming field, such as the Pomelo framework of NetEase.
Web rendering applications: applications that use stable interfaces to improve the rendering performance of web pages.
Consistent frontend and backend programming environments: applications that allow frontend developers to take on server-side development, such as the full-stack JavaScript MongoDB, Express.js, AngularJS, and Node.js. (MEAN) framework.
Prerequisites
An ECS instance is created and meets the following requirements:
The instance is assigned a public IP address by the system or is associated with an elastic IP address (EIP). For information about how to associate an EIP with an instance, see Associate or disassociate an EIP.
The operating system is Alibaba Cloud Linux 3, Ubuntu 18.x or later, or Debian.
An inbound rule is added to the security groups of the instance to open port 22. For information about how to add an inbound security group rule, see Add a security group rule.
Step 1: Deploy a Node.js environment
Node Version Manager (NVM) can be used to install multiple Node.js versions. NVM is version management software of Node.js. You can use NVM to easily switch between Node.js versions.
Connect to the ECS instance.
For more information, see Connect to a Linux instance by using a password or key.
Install the distributed version management system Git.
Alibaba Cloud Linux 3
sudo yum install git -y
Ubuntu or Debian
sudo apt update sudo apt install git -y
Use Git to clone the source code of NVM to the local ~/.nvm directory and check for the latest update.
NoteThe source code of NVM may fail to be cloned due to network issues. If a failure occurs, we recommend that you reclone the source code.
git clone https://gitee.com/mirrors/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
Run the following commands in sequence to configure the environment variables of NVM:
sudo sh -c 'echo ". ~/.nvm/nvm.sh" >> /etc/profile' source /etc/profile
Run the following command to set the npm image repository to the Alibaba Cloud image repository to speed up the download of Node.js:
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
Run the following command to check the version of Node.js:
nvm list-remote
Install multiple Node.js versions.
Install Node.js v20.15.1.
nvm install v20.15.1
Install Node.js v18.20.4.
nvm install v18.20.4
Run the following command to check the installed Node.js versions:
nvm ls
If the following sample command output is returned, v18.20.4 and v20.15.1 are installed and v18.20.4 is being used.
NoteYou can run the nvm use <Version number> command to switch between the versions of Node.js. For example, to switch to Node.js v18.0.0, run the nvm use v18.0.0 command.
Step 2: Deploy a test project
Create a test project file named
example.js
.Run the following command to go back to the home directory:
cd
Run the following command to create the example.js test project file:
touch example.js
Modify the
example.js
project file.Run the following command to open the
example.js
file:vim example.js
Press the I key to enter Insert mode and add the following content to the example.js file.
In this example, port 3000 is occupied by the project, and the command output is Hello World. You can configure the project content (res.end) and a port number (const port) based on your business requirements.
const http = require('http'); const hostname = '0.0.0.0'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
After you add the preceding content, press the Esc key to exit Insert mode. Enter
:wq
and press the Enter key to save and close the file.
Run the following command to run the project and obtain the port number of the project:
node ~/example.js &
Run the following command to list the ports on which the system is listening:
netstat -tpln
In this example, port 3000 is displayed in the command output, which indicates that the project is running as expected.
Add an inbound rule to a security group of the ECS instance to open the specified port.
In this example, port 3000 is used. For information about how to add rules to a security group, see Add a security group rule.
On your Windows computer or a Windows computer that has access to the Internet, open a browser and enter
http://<Public IP address of the ECS instance>:<Port number>
in the address bar.In this example, <Port number> is set to 3000. The following page is displayed when you access the project.