Quickly Deploying Express Framework

Last updated: 2021-08-26 15:23:13

    Overview

    This document describes how to quickly deploy a local Express project to the cloud through a web function.

    Note:

    This document mainly describes how to deploy in the console. You can also complete the deployment on the command line. For more information, please see Deploying Framework on Command Line.

    Prerequisites

    Before using SCF, you need to sign up for a Tencent Cloud account and complete identity verification first.

    Directions

    Template deployment - quick deployment of Express project

    1. Log in to the SCF console and click Function Service on the left sidebar.
    2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
    3. Select Template for Creation Method, enter WebFunc in the search box to filter all web function templates, select Express Framework Template, and click Next as shown below:
    4. On the Configuration page, you can view and modify the specific configuration information of the template project.
    5. Click Complete. After creating the web function, you can view its basic information on the Function Management page.
    6. You can access the deployed Express project at the access path URL generated by API Gateway. Click Trigger Management on the left to view the access path as shown below:
    7. Click the access path URL to access the Express project.

    Custom deployment - quick migration of local project to cloud

    Prerequisites

    The Node.js runtime environment has been installed locally.

    Local development

    1. Run the following command to install the Express framework and express-generator scaffold and initialize the sample Express project.

      npm install express --save
      npm install express-generator --save
      express WebApp
      
    2. Run the following command to enter the project directory and install the required dependencies:

      cd WebApp
      npm install
      
    3. After the installation is completed, run the following command to directly start the service locally.

      npm start
      
    4. Visit http://localhost:3000 in a browser, and you can access the sample Express project locally.

    Cloud deployment

    Next, perform the following steps to make simple modifications to the initialized project, so that it can be quickly deployed through a web function. The project transformation here is usually divided into the following two steps:

    • Change the listening address and port to 0.0.0.0:9000.
    • Add the scf_bootstrap bootstrap file.

    The specific steps are as follows:

    1. In the sample Express project, you can specify the listening address and port through the environment variable in the ./bin/www file. If you don't specify it, port 3000 will be listened on by default as shown below:
    2. Create the scf_bootstrap bootstrap file in the project root directory and add the following content to it (which is used to configure environment variables and start services):
      #!/bin/bash
      export PORT=9000
      npm run start
    3. After the creation is completed, you need to run the following command to modify the executable permission of the file. By default, the permission 777 or 755 is required for it to start normally. Below is the sample code:
      chmod 777 scf_bootstrap
    4. After the local configuration is completed, run the following command to start the file (with execution in the scf_bootstrap directory as an example) and make sure that your service can be normally started locally.
      ./scf_bootstrap
    5. Log in to the SCF console and click Function Service on the left sidebar.
    6. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
    7. Select Custom Creation for Creation Method and configure the options as prompted as shown below:
      • Function Type: select Web function.
      • Function Name: enter the name of your function.
      • Region: enter your function deployment region, which is Guangzhou by default.
      • Runtime Environment: select Nodejs 12.16.
      • Deployment Method: select Code deployment and upload your local project.
      • Submitting Method: select Local folder.
      • Function Code: select the specific local folder where the function code is.
    8. Click Complete.

    Development management

    After the deployment is completed, you can quickly access and test your web service in the SCF console and try out various features of SCF, such as layer binding and log management. In this way, you can enjoy the advantages of low cost and flexible scaling brought by the serverless architecture.