【云开发】本地部署云函数到云开发

180 阅读2分钟

一、初始化云函数

新建 package.json 配置文件

{
  "name": "helloworld",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {},
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@cloudbase/node-sdk": "1.1.1"
  }
}

新建 index.js 云函数

const cloud = require("@cloudbase/node-sdk");

exports.main = async (event, context) => {
  const app = cloud.init({
    env: cloud.SYMBOL_CURRENT_ENV
  });

  return {
	code: 200,
	success: true,
	data: {
		"tcb": 666666666666666
	},
    event,
    envId: cloud.parseContext(context).namespace,
    region: process.env.TENCENTCLOUD_REGION
  };
};

二、本地调试 npm run install 

三、新建云函数配置文件 cloudbaserc.json

1、cloudbaserc.json 配置:

{
  "version": "2.0",
  "envId": "dev-bug",
  "$schema": "https://framework-1258016615.tcloudbaseapp.com/schema/latest.json",
  "framework": {
    "name": "nuxt-ssr",
    "plugins": {
      "client": {
        "use": "@cloudbase/framework-plugin-nuxt",
        "inputs": {}
      },
      "server": {
        "use": "@cloudbase/framework-plugin-function",
        "inputs": {
          "functionRootPath": "cloudfunctions",
          "functions": [
            {
              "name": "nuxt-ssr-echo",
              "timeout": 5,
              "envVariables": {},
              "runtime": "Nodejs12.16",
              "memory": 256,
              "aclRule": {
                "invoke": true
              }
            }
          ],
          "servicePaths": {
            "nuxt-ssr-echo": "/nuxt-ssr-echo"
          }
        }
      },
      "auth": {
        "use": "@cloudbase/framework-plugin-auth",
        "inputs": {
          "configs": [
            {
              "platform": "NONLOGIN",
              "status": "ENABLE"
            }
          ]
        }
      }
    }
  },
  "functionRoot": "./cloudfunctions",  //本地所有云函数文件夹名
  "functions": [
		{
			"name": "nuxt-ssr-echo",
			"timeout": 5,
			"envVariables": {},
			"runtime": "Nodejs12.16",
			"memory": 256,
			"aclRule": {
				"invoke": true
			}
		}
	],
  "region": "ap-shanghai"
}

2、函数配置:

{
  // 关联环境 ID
  "envId": "dev-xxxx",
  // 函数配置
  "functions": [
    {
      // functions 文件夹下函数文件夹的名称,即函数名
      "name": "app",
      // 超时时间,单位:秒 S
      "timeout": 5,
      // 环境变量
      "envVariables": {
        "key": "value"
      },
      // 私有网络配置,如果不使用私有网络,可不配置
      "vpc": {
        // vpc id
        "vpcId": "vpc-xxx",
        // 子网 id
        "subnetId": "subnet-xxx"
      },
      // 运行时,目前可选运行包含:Nodejs8.9, Nodejs10.15, Php7, Java8
      // 此参数可以省略,默认为 Nodejs10.15
      "runtime": "Nodejs10.15",
      // 是否云端安装依赖,仅支持 Node.js 项目
      "installDependency": true,
      // 函数触发器,说明见文档: https://cloud.tencent.com/document/product/876/32314
      "triggers": [
        {
          // name: 触发器的名字
          "name": "myTrigger",
          // type: 触发器类型,目前仅支持 timer (即定时触发器)
          "type": "timer",
          // config: 触发器配置,在定时触发器下,config 格式为 cron 表达式
          "config": "0 0 2 1 * * *"
        }
      ],
      // 函数处理入口,Node.js 和 PHP 项目可以省略,默认值为 index.main
      // 因 Java 的 handler 配置较为特殊,所以当运行时为 Java 时,handler 不能省略
      // 如:package.Class::mainHandler
      "handler": "index.main",
      // fn invoke 本地触发云函数时的调用参数
      "params": {},
      // 部署/更新云函数时忽略的文件
      "ignore": [
        // 忽略 markdown 文件
        "*.md",
        // 忽略 node_modules 文件夹
        "node_modules",
        "node_modules/**/*"
      ]
    }
  ]
}