安装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
。