All Products
Search
Document Center

:Deploy an Egg.js application to Function Compute

Last Updated:Dec 12, 2023

This topic describes how to deploy an Egg.js application to Function Compute. Function Compute provides a method different from traditional deployment methods. You can deploy a traditional Egg.js application to Function Compute and use the application for production without performing operations such as purchasing a server. The deployed Egg.js application can use the auto scaling, pay-as-you-go, and O&M-free features of Function Compute.

Prerequisites

The following operations are complete:

Important

The content about how to use Funcraft to migrate an application from a traditional framework is no longer maintained. We recommend that you use Serverless Devs to migrate relevant frameworks to Function Compute.

For more information about how to migrate Function Compute-related resources from Funcraft to Serverless Devs, see Migrate resources from Funcraft to Serverless Devs.

For more information about how to use Serverless Devs to migrate an application from a web framework, see Use Serverless Devs to deploy web frameworks.

We apologize for any inconvenience caused.

Background information

Egg.js is designed to build enterprise frameworks and applications, and provides developers with core features for web development and a plug-in mechanism that has high extensibility. The developers develop applications based on a unified agreement. This reduces development and maintenance costs. For more information, see What is Egg?

Procedure

Example 1: Deploy a HelloWorld application

  1. Run the following command in the specified directory to create an Egg.js project: For more information, see Quick Start.

    npm init egg --type=simple
    Note

    If an Egg.js project already exists, skip this step.

    Sample command output:

    Need to install the following packages:
      create-egg
    Ok to proceed? (y) y
    npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
    npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
    npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
    [egg-init] use registry: https://registry.npmmirror.com
    [egg-init] target dir is /test/egg-example
    [egg-init] fetching npm info of egg-init-config
    ? Please select a boilerplate type simple - Simple egg app boilerplate
    [egg-init] use boilerplate: simple(egg-boilerplate-simple)
    [egg-init] fetching npm info of egg-boilerplate-simple
    [egg-init] downloading https://registry.npmmirror.com/egg-boilerplate-simple/download/egg-boilerplate-simple-3.3.1.tgz
    [egg-init] extract to /tmp/egg-init-boilerplate
    [egg-init] collecting boilerplate config...
    ? project name egg-example
    ? project description
    ? project author
    ? cookie security keys 1632377928307_4634
    [egg-init] write to /test/egg-example/.autod.conf.js
    [egg-init] write to /test/egg-example/.eslintignore
    [egg-init] write to /test/egg-example/.eslintrc
    [egg-init] write to /test/egg-example/README.md
    [egg-init] write to /test/egg-example/.gitignore
    [egg-init] write to /test/egg-example/package.json
    [egg-init] write to /test/egg-example/app/router.js
    [egg-init] write to /test/egg-example/config/config.default.js
    [egg-init] write to /test/egg-example/config/plugin.js
    [egg-init] write to /test/egg-example/app/controller/home.js
    [egg-init] write to /test/egg-example/test/app/controller/home.test.js
    [egg-init] usage:
          - cd /test/egg-example
          - npm install
          - npm start / npm run dev / npm test
  2. Run the following command to install dependencies:

    npm install

    Sample command output:

    npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
    npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
    npm WARN deprecated debug@3.2.6: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
    npm WARN deprecated babel-eslint@8.2.6: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates.
    npm WARN deprecated mkdirp@0.5.4: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
    npm WARN deprecated mkdirp@0.5.1: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
    npm WARN deprecated core-js@2.6.12: core-js@<3.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
    
    added 1244 packages in 34s
  3. Run the on-premises project.

    npm run dev
  4. Run the fun deploy -y command to deploy the application to Function Compute.

    fun deploy -y

    The following result is returned:

    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 logs to find the temporary domain name that is generated by Function Compute. You can use the 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. You must use a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more information, see Configure a custom domain name.

Example 2: Deploy an open source Egg.js web application

  1. Run the following command to clone the sample project to your on-premises machine:

    git clone https://github.com/OrangeXC/mtime
  2. Run the following commands to go to the specified directory and install dependencies.

    1. Run the following command to go to the mtime directory:

      cd mtime
    2. Run the following command to install the dependencies:

      npm install
  3. Open the config/config.default.js file on your on-premises machine and set the username and password parameters for a MySQL database.

    When you start the application on your on-premises machine, you must use the database configuration in the config/config.default.js file.

    Note

    When you start the application in a production environment, Egg.js uses the database configuration in the config/config.prod.js first by default. Therefore, you must specify the database information of the production environment in this file. After the configuration is complete, you can run the npm run start command to start the application and the npm run stop command to stop the application in production mode to check whether the configuration is valid.

  4. Run the on-premises project.

    npm run dev
  5. Run the following commands to modify the config/config.prod.js file to obtain the read permissions on 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

    The following result is returned:

    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 logs to find the temporary domain name that is generated by Function Compute. You can use the 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. You must use a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more information, see Configure a custom domain name.