This topic describes how to deploy Egg.js applications in Function Compute. Compared with the traditional deployment method, you can skip the steps such as purchasing the machine and deploy an Egg.js application to the remote end for production. Features such as auto scaling, pay-as-you-go, and maintenance-free are provided.

Background information

Egg.js is designed for enterprise-level frameworks and applications. It provides developers with core features of Web development and a plug-in mechanism with high scalability. Developers follow a unified agreement to develop applications, reducing development and maintenance costs. For more information about Egg, see Egg.js.

Step 1: Prepare the environment

You do not need to install Docker but only Funcraft. The easiest way to install Funcraft is to download the executable binary files.

  1. Install Funcraft. For more information, see Install Funcraft.
  2. Run the fun --version command to check whether the installation is successful.
  3. Configure Funcraft. For more information, see Configure Funcraft.

Step 2: Deploy an application

Example 1: Deploy a HelloWorld application.

  1. Run the following command to create an Egg.js Project. For more information, see quick initialization. If an Egg.js project exists, skip this step.
    mkdir egg-example 
    cd egg-example
    npm init egg --type=simple
    npm i
  2. Run the following command to run the on-premises application:
  3. Run the fun deploy -y command to deploy the application to Function Compute.

    Funcraft automatically begins to deploy the application.

    $fun deploy -y
    using template: template.yml
    using region: cn-qingdao
    using accountId: ***********3743
    using accessKeyId: ***********Ptgk
    using timeout: 60
    
    Collecting your services information, in order to caculate devlopment changes...
    
    Resources Changes(Beta version! Only FC resources changes will be displayed):
    ... ... ...
           trigger httpTrigger deploy success
       function egg-example deploy success
    service egg-example deploy success
    
    Detect 'DomainName:Auto' of custom domain 'Domain'
    Fun will reuse the temporary domain 17090425-19861144305****.test.functioncompute.com, expired at 2020-04-27 10:27:05, limited by 1000 per day.
    
    Waiting for custom domain Domain to be deployed...
    custom domain Domain deploy success

    After the application is deployed, you can check the logs to find the temporary domain name that Function Compute generated. You can use this temporary domain name to access the deployed application.

    Note The temporary domain name is used only for demonstration and development purposes. It is valid for a limited period of time. To use an application in a production environment, you must bind the application to a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud. For more information, see Bind a custom domain name.

Example 2: The procedure of deploying an open-source Egg.js Web application is described as an example.

For more information about the preview effect of the sample application, see preview.

  1. Run the following command to clone the sample to your local disk:
    git clone https://github.com/OrangeXC/mtime
  2. Run the following commands to enter mtime directory and install dependencies.
    cd mtime
    npm install
  3. Open the config/config. default.js local file, and set the MySQL username and password to the correct values.
    When starting an application locally, you need to use the database configuration in the config/config.default .js file.
    Note By default, Egg.js takes precedence of the config/config.prod.js database configuration when starting an application in the production environment. Therefore, you need to configure the database information of the production environment to the file. After the configuration, locally run the npm run start and npm run stop commands to start or stop the application in production mode to verify whether the configuration is correct.
  4. Run the following command to run the on-premises application:
    npm run dev
  5. Run the following commands to modify the config/config.prod.js file to get the permission to read the Egg.js cache and log directory.
    config.rundir = '/tmp/run',
    config.logger = {
        dir: '/tmp/log',
    }
  6. Run the fun deploy -y command to deploy the application to Function Compute.
    $ fun deploy -y
    using template: template.yml
    using region: cn-qingdao
    using accountId: ***********3743
    using accessKeyId: ***********Ptgk
    using timeout: 60
    
    Collecting your services information, in order to caculate devlopment changes...
    
    Resources Changes(Beta version! Only FC resources changes will be displayed):
    ... ... ...
            trigger httpTrigger deploy success
        function egg-example deploy success
    service egg-example deploy success
    
    Detect 'DomainName:Auto' of custom domain 'Domain'
    Fun will reuse the temporary domain 17090425-19861144305****.test.functioncompute.com, expired at 2020-04-27 10:27:05, limited by 1000 per day.
    
    Waiting for custom domain Domain to be deployed...
    custom domain Domain deploy success

    After the application is deployed, you can check the logs to find the temporary domain name that Function Compute generated. You can use this temporary domain name to access the deployed application.

    Note The temporary domain name is used only for demonstration and development purposes. It is valid for a limited period of time. To use an application in a production environment, you must bind the application to a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud. For more information, see Bind a custom domain name.