uniCloud云函数概述---公用模块
公用模块可以被普通云函数、云对象调用。它存放在cloudfunctions/comon文件夹内。
一、创建公用模块
打开项目,右击cloudfunctions/comon文件夹,选择“新建公用模块”
创建好的公共模块包含二个文件:
index.js //公共模块文件
package.json //配置文件
二、公用模块模板
A)公用模块是一个JSON对象
// common/hello-common/index.js function getVersion() { return '0.0.1' } module.exports = { getVersion, secret: 'your secret' }
B)公用模块是一个函数
// common/hello-common/index.js module.exports = function(e){ return e }
三、使用公用模块
步骤一:在普通云函数或者云对象上文件夹上右击,选择“管理公共模块或扩展库依赖”


步骤二:在普通云函数或云对象代码内,引入此公共模块( require ):
// use-common/index.js 'use strict'; const { secret, getVersion } = require('hello-common') //引入公用模块: hello-common,此时引入的是JSON对象 exports.main = async (event, context) => { let version = getVersion() return { secret, version } }
// use-common/index.js 'use strict'; const echo = require('hello-common') //引用公用模块,此时引入的是函数对象 exports.main = async (event, context) => { let eventEcho = echo(event) return { eventEcho } }
公用模块中也可以调用其它公用模块,方法和云对象引用公用模块相同。

浙公网安备 33010602011771号