腾讯云函数serverless笔记
发布日期:2021-05-07 00:07:47 浏览次数:38 分类:原创文章

本文共 1926 字,大约阅读时间需要 6 分钟。

一、腾讯云serverless搭建Mysql数据库访问接口

/**************************************************Node8.9-MysqlReference: mysql api---https://www.npmjs.com/package/mysqlReference: How to access database---https://cloud.tencent.com/document/product/236/3130Reference: How to connect api gateway with scf---https://cloud.tencent.com/document/product/628/11983***************************************************/function wrapPromise(connection, sql) {     return new Promise((res, rej) => {       connection.query(sql, function(error, results, fields) {         if (error) {           rej(error)      }      res(results)    })  })}exports.main_handler = async (event, context, callback) => {     const mysql = require('mysql');  const connection = mysql.createConnection({       host: '***', // The ip address of cloud database instance, 云数据库实例ip地址    port: 3306,    user: '***', // The name of cloud database, for example, root, 云数据库用户名,如root    password: '***', // Password of cloud database, 云数据库密码    database: 'test' // Name of the cloud database, 数据库名称  });  connection.connect();  // get value from apigw   let testreturn = event.queryString["CustomerID"] + " " + event.queryString["CustomerName"]  // const updateSql = `UPDATE Customers SET CustomerName = '${CustomerName}' WHERE CustomerID = ${CustomerID}`  const querySql = `SELECT * from user`  // await wrapPromise(connection, updateSql)  let queryResult = await wrapPromise(connection, querySql)    connection.end();  console.log(queryResult)  return queryResult  // return {resul : testreturn}}

image-20210402163355620

获取url传值:

let testreturn = event.queryString["CustomerID"] + " " + event.queryString["CustomerName"]

二、腾讯云函数搭建nodejs服务器

(一)、首先从模板库中拉取一个express模板

image-20210402205847714

image-20210402205925057

(二)、编辑代码

image-20210402210232917

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gO9veqfz-1617372200403)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210402210340678.png)]

(三)、根据需要安装模块

npm install mysql --save

image-20210402210445639

(四)、触发管理

image-20210402210635527

直接复制进浏览器即可访问,也可根据路由进行跳转到对应的网页

上一篇:SAS-阶乘-do end
下一篇:SAS数据集-排序-SORT

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月03日 12时40分27秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章