This article describes how to build a Node.js environment on the CSDN cloud host instance.
Node.js is an open source platform built on Chrome's JavaScript runtime environment that helps developers build fast and scalable network applications. It runs on the V8 engine and executes JavaScript code outside of the web browser. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient, ideal for data-intensive real-time applications running across distributed devices.
Use the Node.js image to create an instance with one click, eliminating cumbersome installation steps.
In the example steps in this article, the software version information used is as follows. When you use different software versions, you need to adjust the command and parameter configuration according to the actual situation.
- CentOS version: 7.6
- Node.js version: 16.4.2
prerequisite
Already purchased a Linux cloud server. If you have not purchased a cloud server, please refer to the Linux host purchase guide
Steps
Log in to the cloud server
Log in to the Linux host by referring to the documentation
Install Node.js 16
Node.js packages are provided through NodeSource Node.js Binary Distributions and .rpm. Add the repository to the system with the command:
curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -
After configuring the repository on your CentOS server, you can proceed to install Node.js 16 on CentOS
sudo yum install -y nodejs
Confirm you can start a node shell:
$ node
Welcome to Node.js v16.4.2.
Type ".help" for more information.
> .exit
Native plugins that will be used when installing the build:
sudo yum install -y gcc-c++ make
To install the Yarn package manager, run the following command:
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install -y yarn
Execute the following commands in sequence to view Node.js and npm version information.
node -v
npm -v
install pm2
PM2 is the JavaScript runtime's process manager for Node.js.
npm install -g pm2
test environment
Once we have Node.js installed, let's build our first web server. Using the following script, create a file called app.js.
tee app.js<<EOL
const http = require('http');
const hostname = '127.0.0.1';
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}/`);
});
EOL
Then run your web server with
pm2 start app.js
Visit http://localhost:3000 and you will see a message saying "Hello World".
The following are some common commands of pm2
$ pm2 start app.js //启动app.js应用
$ pm2 list // 显示所有进程状态
$ pm2 monit // 监视所有进程
$ pm2 logs // 显示所有进程日志
$ pm2 stop all // 停止所有进程
$ pm2 restart all // 重启所有进程
$ pm2 reload all // 0秒停机重载进程
$ pm2 delete [AppName] //根据应用名关闭并删除应用
$ pm2 startup // 产生init脚本
at last
Congratulations, you have successfully installed Node.js on your CentOS server. For a more comprehensive guide to getting started with Node.js, see the Node.js documentation .