Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use typescript + nodejs to develop in Serverless

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use typescript + nodejs for development in Serverless". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to use typescript + nodejs for development in Serverless" together!

preface

First, let me introduce some of the more important concepts that appear in this article:

Function Compute: Function Compute is an event-driven service. Through function calculation, users do not need to manage the operation of the server, just write code and upload it. Function calculations prepare compute resources and run user code in an Auto Scaling fashion, while users pay only for the resources consumed by the actual code run. For more information, see. Aliyun Serverless VSCode Extension: is a VSCode plug-in for Alibaba Cloud Serverless product Function Compute. This plug-in combines Function Calculation: Function tool and Function Calculation SDK. It is a VSCode graphical development tool for debugging function calculation and operating function calculation resources. Function: Function is a tool to support Serverless application deployment, which helps you easily manage resources such as function computation, API gateway, logging service, etc. It assists you with development, build, and deployment through a resource profile (template.yml). More documentation for Fun.

Target

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

Writing business code using typescript

Typescript code can be debugged locally via Serverless VSCode plug-in

Deploy project code to the cloud via plug-ins

Project example diagram

practice

1. Preparation (optional)

Search for Aliyun Serverless plugins in the VSCode plugin market and install them.

Install and configure Docker according to the tutorial in aliyun/fun.

The purpose of early preparation is to facilitate development and debugging. At present, Alibaba Cloud Function Compute provides command line tools Funcraft and graphical VSCode plug-ins. Docker is installed to debug in a local simulation online environment. If you want to quickly browse the construction of nodejs + typescript projects, you can skip it.

2. environment construction

Configure tsconfig.json

Executing tsc --init will generate tsconfig.json in the project root directory

Configure tsconfig.json as follows:

{ "compilerOptions": { "target": "es5", "module": "commonjs", "noImplicitAny": true, "outDir": "./ dist/", "sourceMap": true }, "include": [ "./ src" ] }

Configure 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 src/index.ts file that reads as follows:

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 to js code

Enter npm run compile in Terminal

After completion, there will be a dist folder in the root directory of the project and dist/index.js, dist/index.js.map files

At this point, we have built a regular typescript project, with tsconfig.json, package.json, src source directory, dist result directory. The src/index.ts file defines a handler method that matches the definition of function entry methods in function evaluation.

3. associative function calculation

Create a new index.js file in the root directory of the project, with the following contents: const { handler } = require ('./ dist/index'); module.exports.handler = (event, context, callback) => { handler(event, context, callback); } Here is another handler method defined, which calls the handler method in the compiled js file.

Create a new template.yml file in the root directory of the project, the contents of which are 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 (i.e. services and functions in function calculation). For details, please refer to the Fun specification document. If Serverless VSCode plug-in is installed, you can try the plug-in's intelligent prompt, as shown in the figure:

At this point, we have successfully integrated typescript projects into function calculations. Our approach is to place the typescript source file in the src directory, place the compiled js file in the dist directory, and finally write the index.js file in the project root directory. The handler function in the file calls the entry function of the compiled index file.

4. Local debugging and deployment

local debugging

Insert breakpoints in index.js and src/index.ts files.

Click the function calculation icon in the left column of VSCode to expand the local resource tree.

Click the debug button to the right of the function name to debug ts source code.

deployment function

Click on the function calculation icon in the left column of VSCode

Right-click the function name in the local resource tree and click the deploy button

remote call function

Click the invoke button to the right of the function name in the remote resource tree

Thank you for reading, the above is the content of "how to use typescript + nodejs for development in Serverless", after learning this article, I believe everyone has a deeper understanding of how to use typescript + nodejs for development in Serverless, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report