Application field
This framework is used to use Serverless platform technology under the server side, can erase the cloud platform details, provide consistent deployment operation and maintenance experience, achieve consistency in the development stage and deployment stage, provide a complete local compilation, debugging, breakpoints and other functions.
Technical background
With Serverless platform technology, a large number of cloud functions and triggers need to be configured, TypeScript development is not supported, and local debugging is difficult. Breakpoint analysis cannot be performed.
This Framework Programme
The core problem encountered above is that due to the particularity and detail differences of cloud platform, the backend using Serverless cannot have a complete local debugging and development experience like the traditional server-side development mode. In order to solve this problem, on the basis of using Express, using routing and forwarding, through serverless-HTTP packaging API interface, the local use of TypeScript language development, compilation, breakpoint debugging and other functions, as well as cloud platform deployment consistency.
To solve the above problems, the following solutions are included
1. Engineering structure division of the project
In the ServerLess project structure, functions folder is divided into SRC code file area, dist compilation result area, Config project configuration area and bin debugging area. Ts files under SRC are compiled to dist using Vscode compilation task, index files under functions are used as the entry in cloud base configuration, and serverless-HTTP is used for packaging, to ensure that the local parameter transmission mode is the same as that on cloud.
module.exports.main = async (event, context) => {
context.callbackWaitsForEmptyEventLoop = false;
const entry = (() = > {
const result = require('./dist/app.js');
// const app = require('express')();
// result = app.use(result);
returnresult; }) ();const serverless = require('serverless-http');
let app = entry;
// support for async load app
if (entry && entry.tcbGetApp && typeof entry.tcbGetApp === 'function') {
app = await entry.tcbGetApp(event, context);
}
return serverless(app)(event, context);
};
Copy the code
2. Introduction of Express
To implement a project that uses only one trigger, use Express for routing and forwarding. Services are divided into Controller layer,Storage layer,Dao layer and Model layer. The Controller layer is a collection of routes differentiated by services. The Storage layer is the database pipeline layer, which encapsulates the cloud database, Redis, and mysql.
3. Compile and debug tasks
Use the pre-start task, compile in CommonJS mode, and expose the interface in the WWW under bin with normalizePort.
{
"version": "0.2.0"."configurations": [{"type": "node"."request": "launch"."name": "Compile debug"."preLaunchTask": "typescript"."program": "${workspaceFolder}/functions/cube/dist/server.js"."cwd": "${workspaceFolder}"."args": [
"${relativeFile}"]."protocol": "inspector"
},
{
"type": "node"."request": "attach"."port": 9229."name": "[xrd_sign] Cloud function local debugging"."entry": "xrd_sign"."cloudbaseLocal": true}}]Copy the code
4. Use the configuration file
For project configuration files, template generator is used to generate JSON configuration files for use by the front and back ends. To ensure that the configuration files are pathindexed, the config folder is placed under the SRC and dist statistics directories.