本文介绍如何将Express应用部署到函数计算。与传统的部署方法相比,您可将传统的Express应用一键部署至远端直接用于生产。跳过购买机器等步骤的同时,还拥有了弹性伸缩、按量付费和免运维等特性。

前提条件

您已完成以下操作:

背景信息

Express是一个基于Node.js平台的极简、灵活的Web应用开发框架,提供一系列强大的特征,帮助您创建各种Web和移动设备应用。

操作步骤

  1. 在目标目录中执行以下命令,初始化项目。
    s init devsapp/start-express -d start-express
    -d用于指定项目名称,本示例以项目名称start-express为例,介绍如何迁移Express到函数计算。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-express@0.0.15.zip file decompression completed
     Do you want to install dependencies? No
     please select credential alias default
    
         _____
        |  ___|
        | |____  ___ __  _ __ ___  ___ ___
        |  __\ \/ / '_ \| '__/ _ \/ __/ __|
        | |___>  <| |_) | | |  __/\__ \__ \
        \____/_/\_\ .__/|_|  \___||___/___/
                  | |
                  |_|
    
    
        Welcome to the start-express application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
         This application can help you quickly deploy the Express project:
             Full yaml configuration : https://github.com/devsapp/express#%E5%AE%8C%E6%95%B4yaml
             Express development docs: https://www.expressjs.com.cn/4x/api.html
         This application homepage: https://github.com/devsapp/start-express
    
    
    ‍ Thanks for using Serverless-Devs
     You could [cd /test/start-express] and enjoy your serverless journey!
    ️ If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
  2. 执行以下命令,进入项目目录:
    cd start-express
  3. 执行以下命令,部署项目:
    s deploy
    输出示例:
    [2021-09-03T10:48:50.558] [INFO ] [S-CLI] - Start ...
    [2021-09-03T10:48:50.567] [INFO ] [S-CLI] - Start the pre-action
    [2021-09-03T10:48:50.568] [INFO ] [S-CLI] - Action: npm install --production
    npm WARN app-express@0.0.1 No repository field.
    
    up to date in 0.463s
    [2021-09-03T10:48:51.442] [INFO ] [S-CLI] - End the pre-action
      Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
    [2021-09-03T10:48:52.181] [WARN ] [EXPRESS] - [start.sh] The file already exists and will be used as the startup file.
      Using web framework type: nas, If you want to deploy with container, you can [s cli fc-default set web-framework container] to switch.
    (node:32096) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
      End of request
      Deployed.
      End of request
    [2021-09-03T10:48:56.897] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-09-03T10:48:56.897] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-03T10:48:56.898] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-03T10:48:56.898] [INFO ] [FC-DEPLOY] - Using accessKeySecret: KdcUEJLoGto****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-03T10:48:57.268] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-express exists
    [2021-09-03T10:48:57.430] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-09-03T10:48:57.577] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-09-03T10:48:57.707] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-09-03T10:48:57.816] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-09-03T10:48:57.936] [INFO ] [FC-DEPLOY] - Using vpcConfig: auto: fc will try to generate related vpc resources automatically
    [2021-09-03T10:48:58.382] [INFO ] [VPC] - Getting vpc: fc-deploy-component-generated-vpc-cn-shenzhen
    [2021-09-03T10:48:58.474] [INFO ] [VPC] - Getting vswitch: fc-deploy-component-generated-vswitch-cn-shenzhen
    [2021-09-03T10:48:58.603] [INFO ] [VPC] - Getting securityGroup: fc-deploy-component-generated-securityGroup-cn-shenzhen
    [2021-09-03T10:48:58.773] [INFO ] [FC-DEPLOY] - Generated vpcConfig:
    securityGroupId: sg-wz92ctigpi221smc****
    vSwitchId: vsw-wz9qnuult4q5g4f7n****
    vpcId: vpc-wz9x9bzs0wtvjgt6n****
    
    [2021-09-03T10:48:58.774] [INFO ] [FC-DEPLOY] - Using nasConfig: auto: fc will try to generate related nas file system automatically
    [2021-09-03T10:49:16.121] [INFO ] [FC-DEPLOY] - Generated nasConfig:
    groupId: 10003
    mountPoints:
      - fcDir: /mnt/auto
        nasDir: /serverless-devs-express
        serverAddr: example.com
    userId: 10003
    
    [2021-09-03T10:49:16.123] [INFO ] [FC-DEPLOY] - Checking Function express exists
    [2021-09-03T10:49:16.229] [WARN ] [FC-DEPLOY] - Image registry.cn-shenzhen.aliyuncs.com/web-framework/nodejs12:0.0.1 dose not exist locally.
    Maybe you need to run 's build' first if it dose not exist remotely.
    [2021-09-03T10:49:16.232] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-express exists
    [2021-09-03T10:49:16.294] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-express exists
    [2021-09-03T10:49:16.400] [INFO ] [FC-DEPLOY] - Creating service: serverless-devs-express
    [2021-09-03T10:49:16.400] [INFO ] [FC-DEPLOY] - Creating function: express
    [2021-09-03T10:49:16.401] [INFO ] [FC-DEPLOY] - Creating triggers: ["serverless-devs-express"]
      Make service serverless-devs-express success.
      Make function serverless-devs-express/express success.
      Make trigger serverless-devs-express/express/serverless-devs-express success.
    [2021-09-03T10:49:19.389] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-express exists
    [2021-09-03T10:49:19.450] [INFO ] [FC-DEPLOY] - Checking Function express exists
    [2021-09-03T10:49:19.569] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-express exists
    [2021-09-03T10:49:19.750] [INFO ] [FC-DEPLOY] - Creating custom domain: express.serverless-devs-express.188077086902****.cn-shenzhen.fc.devsapp.net
    [2021-09-03T10:49:20.364] [INFO ] [FC-DOMAIN] - Creating custom domain: express.serverless-devs-express.188077086902****.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: serverless-devs-express
    [2021-09-03T10:49:20.472] [INFO ] [WEB-FRAMEWORK] - nas component get src is: .
    Packing ...
    file /test/start-express/.s is ignored.
    Package complete.
     Start uploading file: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-express/express/start-express.zip
      File uploaded successfully: /mnt/2bfb748534-tpm20-cn-shenzhen/serverless-devs-express/express/start-express.zip
      upload done
    [2021-09-03T10:50:13.196] [INFO ] [FC-NAS] - unzipping file
    [2021-09-03T10:50:16.431] [INFO ] [FC-NAS] - unzip done
      Try container acceleration
    framework-test:
      region: cn-shenzhen
      serviceName: serverless-devs-express
      functionName: express
      customDomains:
        - >-
          http://express.serverless-devs-express.188077086902****.cn-shenzhen.fc.devsapp.net

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://express.serverless-devs-express.188077086902****.cn-shenzhen.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名