Typescript is already standard for larger Node applications, providing javascript with strong typing armor and improving code quality.
Here is a template for rapid deployment of TS and KOA into Tencent cloud function computing. The warehouse is as follows
- Shfshanyue/Serverless-template-zh: Serverless Framework template and example (faster access)
- If you just want to build a blog
- Hosting static web sites with continuous integration using Netlify
- Deploy and speed up your static website using AliOSS
- Github Actions Introduction and practices for continuous integration
- Develop your first cloud function using the Serverless Framework
- Develop your first Koa application using serverless Component
Quick to use
Use this template to quickly create an application
$ serverless install --url https://github.com/shfshanyue/serverless-template-zh/tree/master/tencent-koa-ts --name koa-server
Copy the code
Update the package as early as possible in project creation, using NPm-check-updates
$ npm run ncu
Copy the code
Develop in a test environment
$ npm run dev
Copy the code
File structure
. ├ ─ ─ dist /# Compile the file, and finally need to upload the directory├ ─ ─ node_modules / ├ ─ ─ app. The tsThe entry file must be named app├─ Package. json ├─ Package. json ├─ Bass Exercises# Serverless configuration file└ ─ ─ tsconfig. JsonCopy the code
app.ts
App.ts is the entry file for your business logic, and you can organize routes, business logic, models, etc., just like other Koa applications.
import Koa from 'koa'
const app = new Koa()
app.use(async (ctx, next) => {
ctx.body = `hello, path: '${ctx.request.path}'`
})
app.listen(3333.(a)= > { console.log('Listening 3333')})module.exports = app
Copy the code
serverless component
The Serverless Component can be thought of as a further abstraction of the FAAS and BAAS resource collection, using @serverless/ Hceh-koa
koa-app:
component: '@serverless/tencent-koa'
inputs:
region: ap-guangzhou
functionName: koa-function
runtime: Nodejs10.15
code: ./dist
functionConf:
timeout: 60
memorySize: 128
apigatewayConf:
protocols:
- https
environment: release
Copy the code
The deployment of
You need to prepare node_modules and compiled JS resources for the production environment before deployment.
To pack #
$ npm install typescript
Compile to JS
$ npm run build
Package the production environment package and move to the dist directory
# predeploy: npm ci --production && rsync -avz node_modules dist/
$ npm run predeploy
# Deploy to Tencent cloud$SLS koa - function [█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █] 100% ETA: | | 0 s Speed: 314.98 k/koa - app:functionName: koa-function
functionOutputs: ap-guangzhou: Name: koa-function Runtime: Nodejs10.15 Handler: serverless-handler. Handler MemorySize: 128 Timeout: 60 Region: ap-guangzhou Namespace: default Description: This is afunctioncreated by serverless component region: ap-guangzhou apiGatewayServiceId: service-dture22u url: https://service-dture22u-1257314149.gz.apigw.tencentcs.com/release/ CNS: (empty array) 11 s holds the koa - holds the appdone
Copy the code
As can be seen from the log, deployment to Tencent cloud only 11s, or very fast
Http invocation
Use NPM run dev directly locally and debug on the local port. In a production environment, HTTP calls are made using the URL provided in the SLS post-deployment log
$ curl https://service-dture22u-1257314149.gz.apigw.tencentcs.com/release/
hello, path: '/'#
Copy the code
disadvantages
Before we get started, a few downsides:
- The deployment is troublesome, you need to compile ts to JS first, and upload only the node_modules required by the production environment (all upload speed is too slow)
- Not supported locally
log
及metrics
, you need to go to Tencent cloud console to view
Because the deployment process is a little more complex, consider rewriting a Serverless Component for TS