Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development Cloud development is similar to .js


May 22, 2021 Mini Program Cloud Development Study Guide


Table of contents


Cloud functions run in a node .js, and we can use nodejs built-in modules in cloud functions and install third-party dependencies using npm to help us develop faster. With the help of some excellent open source projects, we avoid repeating wheels and greatly expand the use of cloud functions compared to smaller terminals

Cloud functions with Nodejs

Since cloud functions are closely related to Nodejs, we need to have some basic knowledge of cloud functions and Node's modules and some basic knowledge of Nodejs. Here are just a few basic concepts, and if you'd like to delve into them in more detail, it's a good idea to go through the official technical documentation for Nodejs:

Technical documentation: Nodejs API Chinese technical documentation

Built-in modules for Nodejs

In the previous we have been exposed to Nodejs fs modules, path modules, these we call Nodejs built-in modules, built-in modules do not need us to use npm install download, you can directly use require introduction:

  1. const fs = require('fs')
  2. const path = require('path')

Nodejs' commonly used built-in modules, as well as features, are available directly in cloud functions:

  • fs module: File directory creation, deletion, query, and file read and write, the following creativeReadStream method is similar to reading a file,
  • Path module: provides some APIs for working with file paths
  • url module: Used to process and parse URLs
  • http module: Used to create a service that can handle and respond to http responses
  • Querystring module: Resolve the query string
  • until module: provides a utility for parsing and formatting URL query strings;
  • Net module: Used to create a stream-based TCP or IPC server
  • Crypto module: Provides encryption capabilities, including a complete package of hash, HMAC, encryption, decryption, signature, and authentication capabilities for OpenSSL

In the cloud function using HTTP request access to third-party services can not be restricted by domain names, that is, do not need to be like a small terminal, to add domain names to the request legitimate domain name, also not limited by http and https, no domain name only IP is available, so cloud functions can be applied in a very many scenarios, that is, can easily invoke third-party services, but also can act as a complex function of the full application of the back end. However, it is important to note that cloud functions are deployed in the cloud, and some local area networks and other terminal communications business can only be carried out in small programs.

Common variables

module、exports、require

require is used to introduce modules, JSON, or local files. Modules can be introduced from node_modules, and local modules or JSON files can be introduced using relative paths, such as ./, that are processed based on the directory name defined by __dirname or the current working directory.

Node modularity follows the commonjs specification, and CommonJs defines modules as follows: module identification (module), module export (exports), module reference (require).

In node, a file is a module that is processed using express and require.

Exports represent export objects generated while the module is running. I f the module is not found by the exact file name, Node.js attempts to reload with the .js, .json, or .node extension. The .js file is parsed as a JavaScript text file, and the .json file is parsed as a JSON text file. The node .file is parsed to a compiled plug-in module loaded through process.dlopen(). M odules prefixed with '/' are the absolute path to the file. F or example, need ('home/marco/foo.js') loads /home/marco/foo.js file. M odules prefixed with './' are relative to the file calling require(). That is, the .js must be in .js directory with the foo in order for need ('./circle') to find it.

module.exports are used to specify what is exported by a module, that is, what can be accessed through require().

  1. // 引入本地模块:
  2. const myLocalModule = require('./path/myLocalModule');
  3. // 引入 JSON 文件:
  4. const jsonData = require('./path/filename.json');
  5. // 引入 node_modules 模块或 Node.js 内置模块:
  6. const crypto = require('crypto');

The module of wx-server-sdk

tcb-admin-node、protobuf、jstslib

Third-party modules

Nodejs has an npm official address

Nodejs library recommendation: awesome Nodejs

When there is no '/, './' or '. When /' starts to represent a file, the module must be a core module or loaded from the node_modules directory, such as the wx-server-sdk loaded from node_modules folder:

  1. const cloud = require('wx-server-sdk')

Lodash Utility Library

Lodash is a consistent, modular, high-performance JavaScript utility library that makes JavaScript easier by making it easier to use data types such as array, number, objects, and string. Lodash's modular approach is ideal for: traversing arrays, objects, and strings;

Technical documentation: Lodash official documentation, Lodash Chinese documentation

Use developer tools to create a new cloud function, such as lodash, and then add the dependency on the latest version of lodash latest at package.json:

  1. "dependencies": {
  2. "lodash": "latest"
  3. }

The code in .js in the index is modified as follows, and here the chunk method of lodash is used to split the array:

  1. const cloud = require('wx-server-sdk')
  2. var _ = require('lodash');
  3. cloud.init({
  4. env: cloud.DYNAMIC_CURRENT_ENV,
  5. })
  6. exports.main = async (event, context) => {
  7. //将数组拆分为长度为2的数组
  8. const arr= _.chunk(['a', 'b', 'c', 'd'], 2);
  9. return arr
  10. }

Right-click lodash cloud function directory, select "Open in terminal", npm install install module after right-click deployment and upload all files. W e can call it in a number of ways (detailed earlier) to get results. Lodash is very useful and useful as a tool, its source code is also well worth learning, and more relevant content needs to go to Github and the official technical documentation to learn more.

On the awesome Nodejs page we learned that there are similar tools libraries such as Ramba, immutable, and Mout, which are highly recommended. With Github's awesome list, we can master the coolest and most useful open source projects with one hand, avoiding collecting our own collections.

Moment time processing

When developing small programs, you often need to format time, deal with relative time, calendar time, and time multilingual issues, this time you can use the more popular momentjs.

Technical documentation: moment official documentation, moment Chinese documentation

Use developer tools to create a new cloud function, such as moment, and then add the dependency on the latest version of the moment at package.json:

  1. "dependencies": {
  2. "moment": "latest"
  3. }

The code in .js is changed as follows, we set the moment area to China, formatting the time as December 23, 2019, 4:13:29 pm style and how many minutes before the relative time:

  1. const cloud = require('wx-server-sdk')
  2. const moment = require("moment");
  3. cloud.init({
  4. env: cloud.DYNAMIC_CURRENT_ENV,
  5. })
  6. exports.main = async (event, context) => {
  7. moment.locale('zh-cn');
  8. time1 = moment().format('MMMM Do YYYY, h:mm:ss a');
  9. time2 = moment().startOf('hour').fromNow();
  10. return { time1,time2}
  11. }

However, the time zone in the cloud function is UTC plus 0, not UTC plus 8, the formatted time and time in the country is 8 hours time difference, we can give the number of hours plus 8, we can also modify the time zone. Cloud functions modify time zones so that we can use timezone dependencies (and mode are the same open source author).

Technical documentation: Timezone technical documentation

Add the dependency on the latest version of moment-timezone latest at package.json, and then modify the corresponding code above for easy use:

  1. const moment = require('moment-timezone');
  2. time1 = moment().tz('Asia/Shanghai').format('MMMM Do YYYY, h:mm:ss a');

Get the public IP

Sometimes we want to be able to get the server's public IP, such as a whitelist for IP address, or want to query the address of the server based on IP, ipify is a free and useful dependence, through which we can also get the cloud function of the server's public NETWORK IP.

Technical documentation: Ipify Github address

Use the developer tool to create a new getip cloud function, then enter the following code and add "ipify": "latest", the latest version of ipify dependency, to the "dependencies" of package.json:

  1. const cloud = require('wx-server-sdk')
  2. const ipify = require('ipify');
  3. cloud.init({
  4. env: cloud.DYNAMIC_CURRENT_ENV,
  5. })
  6. exports.main = async (event, context) => {
  7. return await ipify({ useIPv6: false })
  8. }

Then right-click the getip cloud function root, select Open in the terminal, enter the npm install installation dependency, and then upload and deploy all files. We can call this cloud function on the small terminal, we can get the public IP of the cloud function server, this IP is random and limited a few, repeatedly call getip, you can exhaust all the cloud function server ip.

Maybe you'll need an IP whitelist when using cloud functions to connect databases or use cloud functions to build weChat public numbers in the background, and we can add these ips to the whitelist so that cloud functions can do a lot of things.

Buffer file flow

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. exports.main = async (event, context) => {
  6. const fileID = 'cloud://xly-xrlur.786c-xly-xrlur-1300446086/cloudbase/1576500614167-520.png'
  7. const res = await cloud.downloadFile({
  8. fileID: fileID,
  9. })
  10. const buffer = res.fileContent
  11. return buffer.toString('base64')
  12. }
  1. getServerImg(){
  2. wx.cloud.callFunction({
  3. name: 'downloadimg',
  4. success: res => {
  5. console.log("云函数返回的数据",res.result)
  6. this.setData({
  7. img:res.result
  8. })
  9. },
  10. fail: err => {
  11. console.error('云函数调用失败:', err)
  12. }
  13. })
  14. }
  1. <image width="400px" height="200px" src="data:image/jpeg;base64,{{img}}"></image>

Buffer String

Buffer JSON

Image processing sharp

Sharp is a high-speed image processing library that facilitates image editing operations such as cropping, formatting, rotational transformation, filter addition, picture composition (such as watermarking), picture stitching, etc., and supports JPEG, PNG, WebP, TIFF, GIF and SVG formats. Sharp is used on the cloud function side to process pictures, while cloud storage can act as a bridge between the service side and the small terminal to pass the picture.

Technical documentation: Sharp official technical documentation

Create a new one with developer tools

  1. const cloud = require('wx-server-sdk')
  2. const fs = require('fs')
  3. const path = require('path')
  4. cloud.init({
  5. env: cloud.DYNAMIC_CURRENT_ENV,
  6. })
  7. const sharp = require('sharp');
  8. exports.main = async (event, context) => {
  9. //这里换成自己的fileID,也可以在小程序端上传文件之后,把fileID传进来event.fileID
  10. const fileID = 'cloud://xly-xrlur.786c-xly-xrlur-1300446086/1572315793628-366.png'
  11. //要用云函数处理图片,需要先下载图片,返回的图片类型为Buffer
  12. const res = await cloud.downloadFile({
  13. fileID: fileID,
  14. })
  15. const buffer = res.fileContent
  16. //sharp对图片进行处理之后,保存为output.png,也可以直接保存为Buffer
  17. await sharp(buffer).rotate().resize(200).toFile('output.png')
  18. // 云函数读取模块目录下的图片,并上传到云存储
  19. const fileStream = await fs.createReadStream(path.join(__dirname, 'output.png'))
  20. return await cloud.uploadFile({
  21. cloudPath: 'sharpdemo.jpg',
  22. fileContent: fileStream,
  23. })
  24. }

You can also let sharp do not need to first to toFile into a picture, but directly into buffer, so that you can be directly as a parameter to fileContent uploaded to cloud storage, such as:

  1. const buffer2 = await sharp(buffer).rotate().resize(200).toBuffer();
  2. return await cloud.uploadFile({
  3. cloudPath: 'sharpdemo2.jpg',
  4. fileContent: buffer2,
  5. })

Connect to the database MySQL

The public network is connected to the database MySQL

Technical documentation: Sequelize

  1. const sequelize = new Sequelize('database', 'username', 'password', {
  2. host: 'localhost', //数据库地址,默认本机
  3. port:'3306',
  4. dialect: 'mysql',
  5. pool: { //连接池设置
  6. max: 5, //最大连接数
  7. min: 0, //最小连接数
  8. idle: 10000
  9. },
  10. });
  1. 无论是MySQL,还是PostgreSQLRedisMongoDB等其他数据库,只要我们在

Private network connection MySQL

By default, functions developed by the cloud are deployed on a public network and can only access the public network. If developers need access to resources such as Redis, TencentDB, CVM, Kafka, etc. of Tencent Cloud, they need to establish a private network to ensure data security and connectivity security.

Connect to the database Redis

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const Redis = require('ioredis')
  6. const redis = new Redis({
  7. port: 6379,
  8. host: '10.168.0.15',
  9. family: 4,
  10. password: 'CloudBase2018',
  11. db: 0,
  12. })
  13. exports.main = async (event, context) => {
  14. const wxContext = cloud.getWXContext()
  15. const cacheKey = wxContext.OPENID
  16. const cache = await redis.get(cacheKey)
  17. if (!cache) {
  18. const result = await new Promise((resolve, reject) => {
  19. setTimeout(() => resolve(Math.random()), 2000)
  20. })
  21. redis.set(cacheKey, result, 'EX', 3600)
  22. return result
  23. } else {
  24. return cache
  25. }
  26. }

QR code qrcode

Technical documentation: node-qrcode Github address

Message processing

Technical documentation: Nodemailer Github address, Nodemailer official documentation

Use developer tools to create a cloud function, such as nodemail, and then add the dependency on the latest version of nodemailer's latest at package.json:

  1. "dependencies": {
  2. "nodemailer": "latest"
  3. }

Send mail server: smtp.qq.com, use SSL, port number 465 or 587

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. exports.main = async (event, context) => {
  6. const nodemailer = require("nodemailer");
  7. let transporter = nodemailer.createTransport({
  8. host: "smtp.qq.com", //SMTP服务器地址
  9. port: 465, //端口号,通常为465,587,25,不同的邮件客户端端口号可能不一样
  10. secure: true, //如果端口是465,就为true;如果是58725,就填false
  11. auth: {
  12. user: "[email protected]", //你的邮箱账号
  13. pass: "你的QQ邮箱授权码" //邮箱密码,QQ的需要是独立授权码
  14. }
  15. });
  16. let message = {
  17. from: '来自李东bbsky <[email protected]>', //你的发件邮箱
  18. to: '你要发送给谁', //你要发给谁
  19. // cc:'', 支持cc 抄送
  20. // bcc: '', 支持bcc 密送
  21. subject: '欢迎大家参与云开发技术训练营活动',
  22. //支持text纯文字,html代码
  23. text: '欢迎大家',
  24. html:
  25. '<p><b>你好:</b><img src="https://hackwork-1251009918.cos.ap-shanghai.myqcloud.com/handbook/html5/weapp.jpg" rel="external nofollow" /></p>' +
  26. '<p>欢迎欢迎<br/></p>',
  27. attachments: [ //支持多种附件形式,可以是String, Buffer或Stream
  28. {
  29. filename: 'image.png',
  30. content: Buffer.from(
  31. 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
  32. '//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
  33. 'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC',
  34. 'base64'
  35. ),
  36. },
  37. ]
  38. };
  39. let res = await transporter.sendMail(message);
  40. return res;
  41. }

Excel document processing

Excel is a more common format for storing data, so how do you give cloud functions the ability to read and write Excel files? We can search for the keyword "Node Excel" on Github to filter Star more, the conditions are more enough.

Github address: node-xlsx

Create a new cloud function with developer tools and add the latest version of node-xlsx to package.json:

  1. "dependencies": {
  2. "wx-server-sdk": "latest",
  3. "node-xlsx": "latest"
  4. }

Read Excel files stored in the cloud

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const xlsx = require('node-xlsx');
  6. const db = cloud.database()
  7. exports.main = async (event, context) => {
  8. const fileID = 'cloud://xly-xrlur.786c-xly-xrlur-1300446086/china.csv'
  9. const res = await cloud.downloadFile({
  10. fileID: fileID,
  11. })
  12. const buffer = res.fileContent
  13. const tasks = []
  14. var sheets = xlsx.parse(buffer);
  15. sheets.forEach(function (sheet) {
  16. for (var rowId in sheet['data']) {
  17. console.log(rowId);
  18. var row = sheet['data'][rowId];
  19. if (rowId > 0 && row) {
  20. const promise = db.collection('chinaexcel')
  21. .add({
  22. data: {
  23. city: row[0],
  24. province: row[1],
  25. city_area: row[2],
  26. builtup_area: row[3],
  27. reg_pop: row[4],
  28. resident_pop: row[5],
  29. gdp: row[6]
  30. }
  31. })
  32. tasks.push(promise)
  33. }
  34. }
  35. });
  36. let result = await Promise.all(tasks).then(res => {
  37. return res
  38. }).catch(function (err) {
  39. return err
  40. })
  41. return result
  42. }

Save the data in the database as CSV

Technical documentation: json2CSV

HTTP processing

got、superagent、request、axios、request-promise

Although the nodejs version of the cloud function is relatively low (currently 8.9), the vast majority of modules can be tested using the Nodejs 12 or 13 environment, but sometimes note that some modules do not support 8.9, such as got 10.0.1 or more.

In node, the http module can also be used as a client (send a request), and the third-party module request encapsulates its use method for easier operation! So let's introduce the request module

Get request

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const rp = require('request-promise')
  6. exports.main = async (event, context) => {
  7. const options = {
  8. url: 'https://news-at.zhihu.com/api/4/news/latest',
  9. json: true,
  10. method: 'GET',
  11. };
  12. return await rp(options)
  13. }

Post request

Combine file streams

  1. request('https://www.jmjc.tech/public/home/img/flower.png').pipe(fs.createWriteStream('./flower.png')) // 下载文件到本地

Decrypt Crypto

The crypto module is one of the core modules of nodejs and provides security-related features, including a complete package of hash, HMAC, encryption, decryption, signature, and authentication capabilities for OpenSSL. Since the crypto module is a built-in module, we introduced it without downloading it.

Using developer tools to create a new cloud function, such as crypto, and enter the following code in index.js, let's look at which crypto supports encryption algorithms and take MD5 encryption as an example:

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const crypto = require('crypto');
  6. exports.main = async (event, context) => {
  7. const hashes = crypto.getHashes(); //获取crypto支持的加密算法种类列表
  8. //md5 加密 CloudBase2020 返回十六进制
  9. var md5 = crypto.createHash('md5');
  10. var message = 'CloudBase2020';
  11. var digest = md5.update(message, 'utf8').digest('hex');
  12. return {
  13. "crypto支持的加密算法种类":hashes,
  14. "md5加密返回的十六进制":digest
  15. };
  16. }

After deploying the cloud function, we can see from the returned results that the cloud function crypto module supports 46 encryption algorithms.

Texting

“qcloudsms_js”: “^0.1.1”

  1. const cloud = require('wx-server-sdk')
  2. const QcloudSms = require("qcloudsms_js")
  3. const appid = 1400284950 // 替换成您申请的云短信 AppID 以及 AppKey
  4. const appkey = "a33b602345f5bb866f040303ac6f98ca"
  5. const templateId = 472078 // 替换成您所申请模板 ID
  6. const smsSign = "统计小助理" // 替换成您所申请的签名
  7. cloud.init()
  8. // 云函数入口函数
  9. exports.main = async (event, context) => new Promise((resolve, reject) => {
  10. /*单发短信示例为完整示例,更多功能请直接替换以下代码*/
  11. var qcloudsms = QcloudSms(appid, appkey);
  12. var ssender = qcloudsms.SmsSingleSender();
  13. var params = ["1234", "15"];
  14. // 获取发送短信的手机号码
  15. var mobile = event.mobile
  16. // 获取手机号国家/地区码
  17. var nationcode = event.nationcode
  18. ssender.sendWithParam(nationcode, mobile, templateId, params, smsSign, "", "", (err, res, resData) => {
  19. /*设置请求回调处理, 这里只是演示,您需要自定义相应处理逻辑*/
  20. if (err) {
  21. console.log("err: ", err);
  22. reject({ err })
  23. } else {
  24. resolve({ res: res.req, resData })
  25. }
  26. }
  27. );
  28. })

Use developer tools

  1. wx.cloud.callFunction({
  2. name: 'sendphone',
  3. data: {
  4. // mobile: '13217922526',
  5. mobile: '18565678773',
  6. nationcode: '86'
  7. },
  8. success: res => {
  9. console.log('[云函数] [sendsms] 调用成功')
  10. console.log(res)
  11. },
  12. fail: err => {
  13. console.error('[云函数] [sendsms] 调用失败', err)
  14. }
  15. })