express连接本地mysql
时间:2021-07-01 10:21:17
帮助过:16人阅读
express = require("express");
//引入express
var router = express.Router();
//注意Router首字母大写
var mysql = require(‘mysql‘
);
//1.连接数据库
var db = mysql.createConnection({
host: ‘localhost‘, user: ‘root‘,
password: ‘123456‘, database: ‘user‘
});
router.post("/",
function(req,res,next){
//注意 users.js中定义的路由 的路由要改成跟路由"/"
console.log(req.body)
db.query("select* from user", (err, data) => {
if (err) {
console.log("error!", err);
} else {
console.log("success!", data)
res.json({
data:data
});
}
});
})
module.exports = router;
//导出router
。
express连接本地mysql
标签:users require ase export root sele success body ack