Deploying a Node.js Application to an ECS Instance
In the previous article, we have discussed how to deploy a local Java application directly to an Alibaba Cloud Elastic Compute Service (ECS) instance, and received a lot of feedback from our readers. So to help answer the questions from our readers, we will further introduce how to deploy a Node.js application to an Alibaba Cloud ECS instance in this article.
Develop an Application Locally
This article takes a Node.js application for printing “Hello World” on a Web page as an example to explain the deployment method.
const http = require('http');
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Alibaba Cloud Toolkit: Hello World');
});
server.listen(port, '0.0.0.0', () => {});
The preceding code is a standard Node.js project used to print the string “Hello World” on a Web page.
Install Plug-in
Alibaba Cloud provides an Eclipse-based plug-in to help developers efficiently deploy applications written in the local IDE to ECS instances.
URL of the plug-in: https://www.aliyun.com/product/cloudtoolkit_en
The installation process of this Eclipse-based plug-in is similar to that of a common plug-in, and therefore will not be detailed here.
Configure the Plug-in Preferences
After installing the plug-in, configure the preferences by choosing:
Top menu > Window > Preferences > Alibaba Cloud Toolkit > Accounts
When the following page is displayed, configure the AK and SK of your Alibaba Cloud account to complete the configuration of preferences.( If you are using a RAM user account, enter the AK and SK of the RAM user.)
Deploy the Application
In Eclipse, right-click the project name and choose Alibaba Cloud > Deploy to ECS from the shortcut menu. The following deployment window is displayed:
In the Deployment Configurations dialog box, set the deployment parameters, and click Deploy to complete the initial deployment.
The /root/nodejs-demo/restart.sh file contains the following content:
source ~/.bash_profile
killall node
nohup node /root/nodejs-demo/helloworld.js > nohup.log 2>&1 &
Description of Deployment Parameters
- Deploy File: Two options are available. The option Upload File is used for a Node. js project.
- Maven Build: If Maven is used to build the current project, you can use Alibaba Cloud Toolkit to directly build and deploy the application.
- Upload File: If Maven is not used to build the current project, or a locally packaged deployment file already exists, you can select and directly upload the local deployment file.
- Target Deploy ECS: Select a region from the drop-down list, and then select the ECS instance to be deployed in the region.
- Deploy Location: Enter the deployment path on the ECS instance, for example, /root/nodejs-demo.
- Command: Enter the application startup command, for example, sh /root/nodejs-demo/restart.sh. This parameter specifies the command to be executed after the deployment is completed. For a Node.js program, this is usually the startup command node XXX.js.