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 only need to install Funcraft instead of installing Docker. The simplest method is to download the executable binary files.

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

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. Use the following command to run the local project.
  3. Run the fun deploy -y command to deploy the project to Function Compute.

    Funcraft automatically enters the deployment process.

    $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 deployment is successful, you can see the temporary domain name generated by Function Compute in the log. Through this temporary domain name, you can directly access the just deployed application.

    Note The temporary domain name is only used for demonstration and development and is only valid for a period. For production purposes, bind a domain name that has been filed with Alibaba Cloud.

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 .jsfile.
    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. Use the following command to run the local project.
    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 project 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 deployment is successful, you can see the temporary domain name generated by Function Compute in the log. Through this temporary domain name, you can directly access the just deployed application.

    Note The temporary domain name is only used for demonstration and development and is only valid for a period. For production purposes, bind a domain name that has been filed with Alibaba Cloud.