This article describes how to deploy Node.js on a CVM and create a sample project.
To do this, you need to be familiar with common Linux commands such as Installing Software via YUM in a CentOS Environment and understand the versions of the installed software.
Setting up Node.js involves:
To set up Node.js, you need a Linux CVM. If you have not purchased one yet, see Getting Started with Linux CVMs.
Log in to a Linux instance using WebShell (recommended). You can also use other login methods that you are comfortable with:
wget https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64.tar.xz
Visit the Node.js official website for more information.
tar xvf node-v10.16.3-linux-x64.tar.xz
ln -s /root/node-v10.16.3-linux-x64/bin/node /usr/local/bin/node
ln -s /root/node-v10.16.3-linux-x64/bin/npm /usr/local/bin/npm
Once created, you are able to use node and npm commands in any CVM directory.node -v
npm -v
This process allows you to install multiple Node.js versions. Developers can use this to quickly switch among versions.
yum install -y git
git clone https://github.com/cnpm/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
echo ". ~/.nvm/nvm.sh" >> /etc/profile
source /etc/profile
nvm list-remote
nvm install v6.9.5
nvm install v10.16.3
nvm ls
If the following appears, then the installation is successful and the current version in use is Node.js 10.16.3.nvm use v6.9.5
The following appears:index.js
under the root path.cd ~
vim index.js
index.js
file:const http = require('http');
const hostname = '0.0.0.0';
const port = 7500;
const server = http.createServer((req, res) => {
if (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}/`);
});
This article uses port 7500 in the
index.js
file. You can use other ports as needed.
node index.js
http://CVM_Public_IP:Port
If the following appears, Node.js is installed successfully.If you encounter a problem when using CVM, refer to the following documents for troubleshooting based on your actual situation.
Was this page helpful?