express连接本地mysql

Posted 古墩古墩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了express连接本地mysql相关的知识,希望对你有一定的参考价值。

安装mysql插件:

npm install mysql --save-dev
var 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的主要内容,如果未能解决你的问题,请参考以下文章