Serverless real - + Typescript function calculating Practice

Foreword

First introduced in several important concepts under article appears:

Function calculates (Function Compute) : function computing is an event-driven service, by computing function, users need to manage the operation of the servers, just write code and upload. Function calculates ready to computing resources, and is elastically stretchable run user code, and users only need to pay according to the actual code that runs resources consumed. For more information reference function calculation.
Serverless VSCode Extension Aliyun : Ali cloud Serverless product  function calculates Function Compute  the VSCode plug-in that combines the function computes Funcraft: Funcraft tool and function to calculate the SDK  , is a VSCode graphical development tools and debugging function calculates the operating functions of computing resources .
Funcraft : Funcraft is a tool used to support Serverless application deployment, can help you easily manage function computing, API Gateway, logging services and other resources. It is through a resource configuration file (template.yml), help you develop, build, deploy operation. More Fun document references.

aims

This article intends to Serverless a simple function to calculate project as an example, try to use the typescript + nodejs development, build a simple project, to achieve the following goals small:

  1. Use typescript write business code
  2. Typescript can debug the code by local Serverless VSCode plug
  3. By plug-in project code will be deployed to the cloud

Project example of FIG.

practice

1. preparation (optional)

  • The  aliyun / fun  installed and configured Docker tutorials.

The purpose of preparation is to facilitate development and debugging, currently Ali cloud Function Compute provides command-line tools and graphical VSCode Funcraft plug. Docker is installed in order to debug a local analog line environment, if you want to quickly browse at nodejs + typescript to build the project, you can skip.

2. environment to build

  • Configuration tsconfig.json

    • Execution  tsc --initwill be generated in the project root directory tsconfig.json
    • Configure  tsconfig.json content:
      {
        "compilerOptions": {
          "target": "es5",
          "module": "commonjs",
          "noImplicitAny": true,
          "outDir": "./dist/",
          "sourceMap": true
        },
        "include": [
          "./src"
        ]
      }
  • Placed package.json

    • Execution  npm initwill be generated in the project root directory package.json
    • Configure  package.json content:
      {
        "name": "fc-ts",
        "version": "1.0.0",
        "description": "Function Compute + Typescript",
        "main": "index.js",
        "scripts": {
          "compile": "tsc -p ./"
        }
      }
  • Write business code

    • New src / index.ts document prepared as follows:
      export const handler = (
        event: any,
        context: any,
        callback: (err: any, data: any) => void,
      ) => {
        console.log(new String(event));
        callback(null, 'hello world');
      }
  • The ts code into js code

    • Enter the Terminal npm run compile
    • After the completion of the project will be in the root  dist folder as well as  dist/index.js, dist/index.js.map file

So far, we have built a conventional typescript projects,  tsconfig.json, package.json, src 源码目录, dist 结果目录. Wherein the src/index.ts document handler defines a method that meets the definition of a function entry method of calculation functions.

3. Combination function calculation

  1. In the new project root directory  index.js file, as follows:

    const { handler } = require('./dist/index');
    
    module.exports.handler = (event, context, callback) => {
      handler(event, context, callback);
    }

    Here again we define a handler method, which calls the compiled handler method js file.

  2. In the new project root directory  template.yml file, as follows:

    ROSTemplateFormatVersion: '2015-09-01'
    Transform: 'Aliyun::Serverless-2018-04-03'
    Resources:
      demo: # service name
        Type: 'Aliyun::Serverless::Service'
        Properties:
          Description: This is FC service
        func01: # function name
          Type: 'Aliyun::Serverless::Function'
          Properties:
            Handler: index.handler
            Runtime: nodejs8
            CodeUri: ./
            MemorySize: 1024
            Timeout: 15

    该文件中对我们的资源(即函数计算中的服务以及函数)进行了定义,具体内容可以参考:Fun 规范文档
    如果安装了 Serverless VSCode 插件,那么可以尝试下插件的智能提示,效果如图:

至此,我们就成功的将 typescript 项目结合到了函数计算中。我们的做法是:将 typescript 源码文件放置在 src 目录,将编译后的 js 文件放置在 dist 目录,最后在项目根目录中编写了 index.js 文件,文件中的 handler 处理函数调用了编译后 index 文件的入口函数。

4. 本地调试与部署

  • 本地调试

    • 在 index.js 以及 src/index.ts 文件中插入一些断点。
    • 点击 VSCode 左侧栏目中的函数计算图标,展开本地资源树
    • 点击函数名右侧的调试按钮,即可调试 ts 源代码。

  • 部署函数

    • 点击 VSCode 左侧栏目中的函数计算图标
    • 右键本地资源树中的函数名,点击部署按钮

  • 远端调用函数

    • 点击远端资源树中函数名右侧的调用按钮

总结

目前阿里云 Function Compute 没有原生支持 Typescript,但是通过本文的方式可以做到在本地开发调试时使用 Typescript。接下来总结下这种实践方法的优点和不足:

优点

  1. 使用 Typescript 进行开发
  2. 支持本地调试 Typescript 代码
  3. 项目部署到云端后,可以在云端查看 Typescript 源码

不足

  1. 本地调试时需要在项目根目录的 index.js 文件中插入一个断点。
  2. 更新代码后,在调试以及部署前需要手工进行一次 Typescript 代码到 js 代码的编译。

本文中介绍的实践方式只是一种思路,欢迎大家提供其他的思路。

 

本文作者:泽尘

原文链接

本文为阿里云内容,未经允许不得转载。

Guess you like

Origin www.cnblogs.com/zhaowei121/p/12035691.html