Node.js is a JavaScript runtime environment built on the Chrome V8 JavaScript engine.
You can use Node.js to build online scalable web applications. This topic describes
how to install Node.js and deploy a project on an Elastic Compute Service (ECS) instance
that runs CentOS 7.8.
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 applicable to 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 to consume
existing data.
- Utilities: a variety of utilities from frontend compression and deployment applications
such as grunt to desktop GUI 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.
Step 1: Deploy the Node.js environment
- Connect to the ECS instance.
- Deploy the Node.js environment.
This topic describes two methods used to deploy the Node.js environment. You can use
one of the following methods to deploy the Node.js environment based on your business
requirements:
- Method 1: Use NVM to install multiple Node.js versions
Node Version Manager (NVM) is a software used to manage Node.js versions. You can
use NVM to switch among Node.js versions with ease. NVM is suitable for developers
who are dedicated to Node.js or users who want to efficiently update or switch among
Node.js versions. To install multiple Node.js versions by using NVM, perform the following
operations:
- Install the distributed version management system Git.
yum install git -y
- Use Git to clone the source code of NVM to the local ~/.nvm directory and check for the latest update.
git clone https://github.com/cnpm/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:
echo ". ~/.nvm/nvm.sh" >> /etc/profile
source /etc/profile
- Install multiple Node.js versions.
- Install v6.9.5.
nvm install v6.9.5
- Install v7.4.0.
nvm install v7.4.0
Note You can run the nvm list-remote command to view all versions of Node.js.
- Check the installed Node.js versions.
nvm ls
The following command output indicates that v6.9.5 and v7.4.0 are installed and v7.4.0
is in use:
v6.9.5
-> v7.4.0
system
stable -> 7.4 (-> v7.4.0) (default)
unstable -> 6.9 (-> v6.9.5) (default)
Note You can run the nvm use <Version number> command to switch among the Node.js versions. For example, you can run the nvm use v6.9.5 command to switch to Node.js v6.9.5.
- Method 2: Use binary files to install a Node.js version
The installation package used in this method is a compiled binary file. After you
decompress the package, the node and npm files already exist in the
bin folder. Therefore, you do not need to recompile the binary file. In this example,
Node.js v6.9.5 is installed. Perform the following operations:
- Download the Node.js installation package.
wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz
- Decompress the package.
tar xvf node-v6.9.5-linux-x64.tar.xz
- Run the following commands in sequence to create symbolic links for node and npm.
After you create the symbolic links, you can directly run the node and npm commands
in a directory.
ln -s /root/node-v6.9.5-linux-x64/bin/node /usr/local/bin/node
ln -s /root/node-v6.9.5-linux-x64/bin/npm /usr/local/bin/npm
- Check the versions of node and npm.
node -v
npm -v
Then, the Node.js environment is installed. By default, the software is installed
in the /root/node-v6.9.5-linux-x64/ directory.
If you want to install the software to another directory such as
/opt/node/, run the following commands in sequence.
- Create the /opt/node/ directory.
mkdir -p /opt/node/
- Move all files of Node.js to the /opt/node/ directory.
mv /root/node-v6.9.5-linux-x64/* /opt/node/
- Remove the symbolic links for node and npm from the source directory.
rm -f /usr/local/bin/node
rm -f /usr/local/bin/npm
- Create symbolic links for node and npm in the /opt/node/ directory.
ln -s /opt/node/bin/node /usr/local/bin/node
ln -s /opt/node/bin/npm /usr/local/bin/npm
Step 2: Deploy a test project
- Run the following commands in sequence to create a test project file named example.js.
- Go back to the /root directory.
cd
- 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:
- Press the I key to enter the edit mode and add the following content to the example.js file.
In this example, port 3000 is occupied by the project. The command output is
Hello World. You can configure the project content and a port number 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 the edit mode. Enter
:wq
and press the Enter key to save and close the file.
- Run the project and obtain the port number of the project.
- List the ports on which the system is listening.
In this example, port 3000 is included in the command output, which indicates that
the project is running normally.
- Add an inbound rule to the security group of the ECS instance to allow traffic on
the specified port.
In this example, port 3000 is used. For more 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 can access 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 3000. The following page is displayed when you access the project.
