安装:
npm install mysql --save
var mysql = require('mysql');
// 1.创建连接
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '134658',
database : 'people'
});
// 2.连接数据库
connection.connect();
//3. 执行数据操作
connection.query('SELECT * FROM `user`', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results);
});
// 新增数据
// connection.query(`INSERT INTO user VALUES(null,"admin","123456")`, function (error, results, fields) {
// if (error) throw error;
// console.log('The solution is: ', results);
// });
//4. 关闭连接
connection.end();
补充promise
封装promise-API
var fs =require('fs')
function pReadFile(filePath){
return new Promise(function(resolve,reject){
fs.readfile(filePath,'utf8',function(err,data){
if(err){
reject(err)
}else{
resolve(data)
}
})
})
}
pReadFile('./data/a.txt')
.then(function(data){
console.log(data)
return pReadFile('./data/b.txt')
})
.then(function(data){
console.log(data)
return pReadFile('./data/c.txt')
})
.then(function(data){
console.log(data)
})
这个 可以开启一个接口服务
封装的ajax方法
使用这个 方法
通过嵌套获得两个接口的信息
用promise 封装这个ajax方法
调用这个方法渲染页面
mongoose里所有的API都支持promise
// 用户注册
// 1.判断用户是否存在
// 如果已存在,结束注册
// 如果不存在,注册(保存一条用户信息)
// User.find()
// .then(function(data){
// console.log(data)
// })
// User.findOne({
// username:'aaa'
// })
// .then(function(user){
// if(user){
// // 用户已存在,不能注册
// console.log('用户已存在')
// }else{
// // 用户不存在,可以注册
// return new User({
// username:'aaa',
// password:'123',
// email:'sasass'
// }).save()
// }
// })
// .then(function(ret){
// })