Skip to main content

Node Plugin

Tencent CloudBase Framework Node Plugin

Tencent CloudBase Framework Node Plugin

Github License Npm version issue PRs Welcome star star

CloudBase Framework "Node.js App" Plugin: Using the CloudBase Framework, it deploys Node applications to the CloudBase environment with one click, providing a high-performance Node application service with automatic elastic scaling. It supports underlying deployment as functions or cloud hosting, and integrates with other plugins like the Website plugin and Function plugin to enable cloud-native integrated development.

Features

  • Free from concerns about the underlying architecture: Just focus on developing business services without adapting to functions or containers
  • Cost Savings: Achieves significant resource cost reduction through resource scaling, elastic scaling, and flexible billing.
  • Framework Support: Seamlessly supports projects built with native and frontend frameworks.
    • Native Node.js
    • Express
    • Koa

Usage

Step 1. Preparations

Specific steps please refer to Preparing the Cloud Development Environment and CloudBase CLI Command Tool

Step 2. Enter the Node project directory and initialize

If you have an existing Node application project

cloudbase

If you want to start a new project from scratch, you can directly execute init to begin a project from a template.

cloudbase init

Step 3. One-Click Deployment

cloudbase framework deploy

Configuration

By default, no configuration is required for use. The following configuration parameters are intended for scenarios with specific requirements.

Configuration Example

After running cloudbase init, the Cloud Development configuration file cloudbaserc.json` will be created. You can modify and write plugin configurations in the plugins section of the configuration file.

{
"envId": "{{envId}}",
"framework": {
"plugins": {
"server": {
"use": "@cloudbase/framework-plugin-node",
"inputs": {
"entry": "app.js",
"path": "/nodeapp",
"name": "nodeapp"
}
}
}
}
}

Configuration Parameters Description

entry

Default app.js

The service entry file for Node, relative to projectPath, must export an instance of app or server. It also supports exporting the tcbGetApp method to asynchronously retrieve the app, where the return value of the method is an instance of app or server.

such as the app.js for a koa service

const Koa = require("koa");
const { router } = require("./routes/");

const app = new Koa();

app.use(router.routes());

module.exports = app;

the app.js for the nest service

const express = require("express");
const { NestFactory } = require("@nestjs/core");
const { ExpressAdapter } = require("@nestjs/platform-express");
const { AppModule } = require("./dist/app.module");

const expressApp = express();
const adapter = new ExpressAdapter(expressApp);

exports.tcbGetApp = async () => {
const app = await NestFactory.create(AppModule, adapter);
await app.init();
return expressApp;
};

path

Required. Access subpath, such as /node-app.

name

Required. Service name, such as node-app.

projectPath

Optional. Specify the directory of the Node service relative to the root directory of the current project.

buildCommand

Supported from version 0.5.x onwards

Optional. Specify the build command, such as npm run build.

platform

Optional. The underlying platform used, supporting container (cloud hosting) and function (cloud function), defaults to function.

containerOptions

Optional. When platform is set to container, additional advanced settings such as CPU and memory can be customized.

Example:

{
"use": "@cloudbase/framework-plugin-node",
"inputs": {
"entry": "app.js",
"path": "/nodeapp",
"name": "nodeapp",
"platform": "container",
"containerOptions": {
"cpu": 2,
"mem": 2
}
}
}

For specific configuration details, refer to @cloudbase/framework-plugin-container configuration

functionOptions

Optional. When platform is set to function, additional advanced settings such as VPC and environment variables can be customized.

Example:

{
"use": "@cloudbase/framework-plugin-node",
"inputs": {
"entry": "app.js",
"path": "/nodeapp",
"name": "nodeapp",
"platform": "function",
"functionOptions": {
"timeout": 5,
"envVariables": {
"TEST_ENV": 1
},
"vpc": {
"vpcId": "xxx",
"subnetId": "xxx"
}
}
}
}

For specific configuration information, please refer to the @cloudbase/framework-plugin-function configuration.

wrapExpress

Optional. When platform is set to function, it supports automatically wrapping the function with an express layer.

Example:

{
"envId": "fx",
"framework": {
"plugins": {
"server": {
"use": "@cloudbase/framework-plugin-node",
"inputs": {
"entry": "./api/index.js",
"path": "/api",
"name": "github-stats-api",
"wrapExpress": true
}
},
"pin": {
"use": "@cloudbase/framework-plugin-node",
"inputs": {
"entry": "./api/pin.js",
"path": "/api/pin",
"name": "github-stats-pin",
"wrapExpress": true
}
}
}
}
}

More Plugins

Please visit the CloudBase Framework Plugin List to use other plugins in combination

Documentation Resources