All Products
Search
Document Center

Elastic Compute Service:Deploy a Node.js environment

Last Updated:Sep 10, 2024

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.

  1. Connect to the ECS instance.

    For more information, see Connect to a Linux instance by using a password or key.

  2. 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
  3. Use Git to clone the source code of NVM to the local ~/.nvm directory and check for the latest update.

    Note

    The 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`
  4. 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
  5. 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
  6. Run the following command to check the version of Node.js:

    nvm list-remote
  7. Install multiple Node.js versions.

    1. Install Node.js v20.15.1.

      nvm install v20.15.1
    2. Install Node.js v18.20.4.

      nvm install v18.20.4
  8. 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.

    image

    Note

    You 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

  1. Create a test project file named example.js.

    1. Run the following command to go back to the home directory:

      cd
    2. Run the following command to create the example.js test project file:

      touch example.js
  2. Modify the example.js project file.

    1. Run the following command to open the example.js file:

      vim example.js
    2. 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}/`);
      });
    3. 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.

  3. Run the following command to run the project and obtain the port number of the project:

    node ~/example.js &
  4. 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.

  5. 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.

  6. 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.