preface

First, a few important concepts emerge from this article:

Function Compute: Function Compute is an event-driven service that lets the user write code and upload it instead of managing the server. Function computing prepares computing resources and runs user code on an elastic scale, with users paying only for the resources consumed by the actual code running. Function calculation refer to more information. Aliyun Serverless VSCode Extension: VSCode Extension for Aliyun Serverless product Function Compute. Funcraft tool and function calculation SDK, is a VSCode graphical development debugging function calculation and operation function calculation resources tool. Funcraft: Funcraft is a tool used to support Serverless application deployment. Funcraft helps you easily manage functions, API gateways, logging services, and other resources. It helps you develop, build, and deploy through a resource configuration file (template.yml). More documentation references for Fun.

The target

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

  1. Write business code in typescript
  2. You can debug typescript code locally through the Serverless VSCode plug-in
  3. Deploy project code to the cloud through plug-ins

Sample project diagram





practice

1. (Optional)

  • Search the Aliyun Serverless plug-in in the VSCode plug-in marketplace and install it.




  • Follow the tutorials in Aliyun/Fun to install and configure Docker.
The purpose of preliminary preparation is to facilitate development and debugging. At present, ali cloud Function Compute provides command line tool Funcraft and graphical VSCode plug-in. Docker is installed for debugging in the local emulated online environment. Skip this if you want a quick look at building nodeJS + typescript projects.

2. Environment construction

  • Configuration tsconfig. Json
  • TSC –init will generate tsconfig.json in the project root directory
  • Set tsconfig.json to:
{ “compilerOptions”: { “target”: “es5”, “module”: “commonjs”, “noImplicitAny”: true, “outDir”: “./dist/”, “sourceMap”: true }, “include”: [ “./src” ] }

  • Configuration package. Json
  • Executing NPM init will generate package.json in the project root directory
  • Configure package.json as follows:
{“name”: “fc-ts”, “version”: “1.0.0”, “description”: “Function Compute + Typescript”, “main”: “index.js”, “scripts”: { “compile”: “tsc -p ./” } }

  • Write business code
  • Create a new SRC /index.ts file and write the following contents:
export const handler = ( event: any, context: any, callback: (err: any, data: any) => void, ) => { console.log(new String(event)); callback(null, ‘hello world’); }

  • Compile TS code into JS code
  • Enter NPM run compile in Terminal
  • The dist folder and dist/index.js and dist/index.js.map files will be in the root directory of the project


Json, package.json, SRC source directory, and dist result directory. The SRC /index.ts file defines a handler method that conforms to the definition of a function entry method in function evaluation.

3. Combine function calculation

  1. Const {handler} = require(‘./dist/index’); module.exports.handler = (event, context, callback) => { handler(event, context, callback); } Here we define a handler method that calls the handler method in the compiled JS file.
  2. In the project root directory for the new template. Yml file, the file content is 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 This file defines our resources (that is, services and functions in function computation), as described in the Fun specification documentation. If the Serverless VSCode plug-in is installed, you can try the intelligent prompt of the plug-in, as shown in the following figure:



At this point, we’ve successfully incorporated typescript projects into function evaluations. What we do is: We place the typescript source file in the SRC directory and the compiled JS file in the dist directory. Finally, we write an index.js file in the project root directory. The handler handler in the file calls the compiled index file entry function.

4. Local debugging and deployment

  • Local debugging
  • Insert breakpoints in index.js and SRC /index.ts files.
  • Click on the function calculation icon in the left-hand column of VSCode to expand the local resource tree
  • Click the debug button to the right of the function name to debug the TS source code.






  • The deployment of function
  • Click on the function calculation icon in the left-hand column of VSCode
  • Right-click the function name in the local resource tree and click the Deploy button






  • Call the function remotely
  • Click the call button to the right of the function name in the remote resource tree






conclusion

Currently, Alicloud Function Compute does not support Typescript natively, but in this way, we can use Typescript for local development and debugging. Next, summarize the advantages and disadvantages of this practice method:

advantages

  1. Develop with Typescript
  2. Supports native debugging of Typescript code
  3. Once your project is deployed to the cloud, you can view the Typescript source code there
insufficient

  1. Local debugging requires a breakpoint to be inserted in the index.js file in the project root directory.
  2. After updating the code, you need to manually compile the Typescript to JS code before debugging and deployment.
The practice described in this article is just one idea, and other ideas are welcome.



The original link

This article is the content of Ali Cloud, shall not be reproduced without permission.