一、mysql的使用

使用mysql首先得有数据库并且表里面有数据,我创建了数据库newsql,里面Tables有表company等等。

  company有id,name,other等字段

1、导入mysql数据库

    我们在config.json中创建了mysql的json,这里在routes文件夹下创建mysql.js链接mysql

在mysql.js中

var mysql = require('mysql');
var config = require('../config.json')
var connection = mysql.createConnection({
host: config.mysql.host,
user: config.mysql.user,
port: config.mysql.port,
password: config.mysql.password,
database: config.mysql.database
}); connection.connect();
module.exports = connection; // 导出mysql

  2、在接口函数中使用mysql增删改查(增删改查在下篇文章中介绍)

var express = require('express');
var router = express.Router();
var mysql = require('./mysql.js') // 导入mysql /* GET users listing. */
router.get('/', function(req, res, next) { // 接口函数get方法
mysql.query('SELECT * FROM company', function (error, results, fields) { // 这里只是简单说明下查询数据,查找actor表里面的所有数据
if (error) throw error; //报错返回
console.log('This: ',results); // 在终端上面打印获得的所有数据
res.json({code:0,data:results}); // 获取数据
 });
}); module.exports = router;//导出到index.js

  重启npm start 刷新localhost:3000可以看到结果

 二、短信获取

短信很简单,官方文档都有介绍,短信函数使用post方法,先贴代码再一个个介绍吧!

  短信需要安装短信插件@alicloud/sms-sdk    npm install @alicloud/sms-sdk --save

var express = require('express');
var router = express.Router();
var SMSClient = require('@alicloud/sms-sdk'); // 短信插件
var config = require('../config.json'); // 获取短信需要的参数
var accessKeyId = config.duanxin.key;
var secretAccessKey = config.duanxin.secret; // 六位随机数
var range=function(start,end)
{
var array=[];
for(var i=start;i<end;++i) array.push(i);
return array;
};
var randomstr = range(0,6).map(function(x){
return Math.floor(Math.random()*10);
}).join('');
router.post('/phoneCode', function (req, res, next) {
var mobile = req.body.mobile // 传来的手机号 var smsClient = new SMSClient({ // 构建发送短信的阿里云用户
accessKeyId,
secretAccessKey
})
smsClient.sendSMS({
PhoneNumbers: mobile,
SignName: config.duanxin.SignName, // 这是手机上显示的短信主题
TemplateCode: config.duanxin.TemplateCode,
TemplateParam: '{"code":"' + randomstr + '"}' // 在手机上显示的短信码
}).then(re => {
if (re.Code === 'OK') {
//发送成功后需要做的事......
} else {
res.json({
code: re.Code,
data: '发送失败'
})
}
})
})

  

  

node中mysql和短信使用方法(3)的更多相关文章

  1. 使用node中mysql模块连接本地数据库

    连接数据库的方法迄今为止学了三种: cmd方式.可视化工具,今天记第三种----node端连接数据库. 一:mysql模块介绍与下载 1.mysql模块是node端专门连接数据库的第三方模块 2.下载 ...

  2. django中mysql数据库设置错误解决方法

    刚在django中settings.py进行设置mysql数据库. 当进行执行python manage.py shell命令时会报以下错误: 只需要在settings.py中 DATABASES = ...

  3. python中mysql主从同步配置的方法

    1)安装mysql ubuntu中安装一台mysql了,docker安装另外一台mysql 获取mysql的镜像,主从同步尽量保证多台mysql的版本相同,我的ubuntu中存在的mysql是5.7. ...

  4. Grafana中mysql作为数据源的配置方法

    需求 近期在使用python写一套模拟API请求的监控项目,考虑数据可视化这方面就采用grafana来呈现,下面来看看怎么弄. 数据源准备 首先安装好mysql,将监控的日志数据写入到mysql之中. ...

  5. CentOs中mysql服务器重置root密码方法

    1. 停止mysql: service mysqld stop 2. 编辑/etc/my.cnf,在[mysqld]这行后面加上skip-grant-tables ,并保存 3. 启动mysql: s ...

  6. node中controller的get和post方法获取参数

    1.get: const body = ctx.query; // get请求   2.post: const body = ctx.request.body; // post请求

  7. mysql 中添加索引的三种方法

    原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...

  8. 常用SQL语句及在node中使用MySQL

    摘要:一些重要的SQL命令 SELECT - 从数据库中提取数据 UPDATE - 更新数据库中的数据 DELETE - 从数据库中删除数据 INSERT INTO - 向数据库中插入新数据 CREA ...

  9. PHP中MySQL数据库连接,数据读写,修改方法

    MySQL连接大的来说有两种方法,一种是mysqli,另一种是mysql.php为连接MySQL提供了函数库,有mysql和mysqli,mysqli是mysql函数库的扩展,是php5才支持的.当你 ...

随机推荐

  1. LeetCode 1: single-number

    Given an array of integers, every element appears twice except for one. Find that single one. soluti ...

  2. Element filtername is not allowed here-web.xml version="3.0"-intellij idea

    Element filtername is not allowed here-web.xml version="3.0"-intellij idea Intellij IDEA 报 ...

  3. 列表渲染.html

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. Python网络爬虫之cookie处理、验证码识别、代理ip、基于线程池的数据爬去

    本文概要 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时, ...

  5. (JavaScript) 字符串转16进制

    function strToBase64() { var str = "https://www.baidu.com/"; var val = ""; for ( ...

  6. phpStorm //todo 的用途

    用phpstorm看到别人的代码使用了注释//todo,且todo是彩色的 我想这个应该是有点用的吧,于是百度了下,大概是可能由于某些原因,导致部分代码没有写.但又怕忘了, 用//todo就可以做提示 ...

  7. 洛谷P3121 【[USACO15FEB]审查(黄金)Censoring (Gold)】

    双栈+AC自动机 这题其实跟一道KMP算法的题有一些渊源,它就是这道题的简单板. Clear: 给你两个串A,B,每次在B串中从左到右找串A,并将该子串删除,直到找不到为止,问你能删几次. 样例输入: ...

  8. 大公司喜欢问的Java集合类面试题

    Collection Collection是基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素而另一 ...

  9. Centos7下安装nexus3.x 安装

    1.官网下载unix版本 2.上传到linux系统的/usr/目录下 [root@lmll70op-ne ~]# cd /usr/ [root@lmll70op-ne usr]# ll 3.解压,并重 ...

  10. Vue使用antV G2制作看板

    工作中需要制作一个看板,选型选用antV G2进行开发. 由于项目前端是使用Vue,于是研究了antVG2在Vue中的使用. 1.安装antv/g2 npm install @antv/g2 --sa ...