1
头图

As a currently popular Web service development language, Node.js provides many related functions that support HTTP scenarios, which can be said to be born for Web construction. Therefore, based on Node.js, a variety of web service frameworks have also been born. They extend the content of Node.js and focus on the direct construction and development of web services, such as Express, Koa, etc., and become the first in the development of web services. Preferred.

The release of the cloud function Web Function also brings a new solution for developers to upload web services to the cloud. Simply modify the listening port to directly deploy the currently popular Node.js framework to the cloud and enjoy the benefits of Serverless technology. Many advantages of free operation and maintenance, low cost, and expansion and contraction on demand.

This document will guide you how to quickly deploy your local Express project to the cloud through Web Function.

01. Template deployment-no need to change business code, one-click deployment

  1. Log in to the Serverless console, click "Function Service" in the left navigation bar, select the region where you want to create a function at the top of the main interface, and click "New" to enter the function creation process.
  2. Choose to use "Template Creation" to create a new function, enter " WebFunc " in the search box, filter all Web function templates, select the Express framework template, and click "Next", as shown in the following figure:

  1. On the "Configuration" page, you can view and modify the specific configuration information of the template project;
  2. Click " complete " to create the function. After the function is created, you can view the basic information of the Web function on the "Function Management" page, and access it through the access path URL generated by the API gateway to view your deployed Express project.

02. Custom deployment-3 steps to quickly migrate local projects to the cloud

1. Local development

  1. First, after ensuring that your local Node.js runtime environment has been installed, install the Express framework and express-generator scaffolding, and initialize your Express sample project
npm install express --save
npm install express-generator --save
express WebApp
  1. Enter the project directory, install the dependency package
cd WebApp
npm install
  1. After the installation is complete, start directly locally, and visit http://localhost:3000 in the browser to complete the access to the Express sample project locally
npm start

2. Deploy to the cloud

Next, we make simple modifications to the initialized project so that it can be quickly deployed through Web Function. The project transformation here is usually divided into two steps:

  • Modify the listening address and port to 0.0.0.0:9000
  • Add scf_bootstrap startup file

Specific steps are as follows:

  1. It is known that in the Express sample project, the ./bin/www . Open the file and you can find that we can set the specified listening port through environment variables, otherwise it will automatically monitor 3000

  1. Next, create a new scf_bootstrap startup file in the project root directory, configure environment variables in it, and specify the service startup command
#!/bin/bash
export PORT=9000
npm run start

After creation, pay attention to modify your executable file permissions. By default, 777 or 755 permissions are required to start normally

chmod 777 scf_bootstrap
  1. After the local configuration is complete, execute the startup file to ensure that your service can be started normally locally. Next, log in to the Tencent Cloud Cloud Function Console and create a new Web function to deploy your Express project:

3. Development Management

After the deployment is complete, you can quickly access and test your web services on the SCF console, and experience multiple features of cloud functions such as layer binding, log management, etc., and enjoy the low cost, flexible expansion and contraction brought by the serverless architecture, etc. Advantage.

Web Function experience

Web Function is currently being released in the "Chengdu Region" gray scale, and other regions will be opened one after another, so stay tuned!

One More Thing

Experience Tencent Cloud Serverless Demo now and receive the Serverless new user gift pack Tencent Cloud Serverless newbie experience .


Serverless
127 声望15 粉丝

Serverless 中文社区是由一群拥有相同价值观和理念的志愿者们共同发起,于 2019 年 12 月正式成立。