node.js: mysql Connection in vscode

mysql script:

drop table `vuedustu`;

CREATE TABLE `vuedustu` (
  `stuId` int(11) NOT NULL AUTO_INCREMENT comment'学生编号',
  `stuname` varchar(255) DEFAULT NULL comment'学姓姓名',
  `stusex` varchar(255) DEFAULT NULL comment'性别',
  `stucollege` varchar(255) DEFAULT NULL comment'院系',
  `stuclass` varchar(255) DEFAULT NULL comment'班级',
  PRIMARY KEY (`stuId`)
) ENGINE=MyISAM comment='学生表' AUTO_INCREMENT=2025807108 DEFAULT CHARSET=utf8;

INSERT INTO `vuedustu` VALUES (2025101101,'陈二','男','农学院','园林25(1)班'),
(2025307101,'李三','男','工程学院','土木25(1)班'),
(2025407101,'张四','男','电气与信息学院','计算机25(1)班'),
(2025407102,'王五','女','电气与信息学院','计算机25(1)班'),
(2025507121,'赵一','女','人文与生命科技学院','生技25(1)班'),
(2025607101,'何六','女','会计学院','会计25(1)班'),
(2025707121,'刘八','男','经济管理学院','经管25(1)班'),
(2025807102,'金九','男','理学院','信息工程25(1)班'),
(2025807105,'银十','女','工学院','自动化工程25(2)班'),
(2025807107,'涂不','女','医学院','临床科学班(3)班');

select * from vuedustu;

  

// 
/**
* db.js
* node 20 vue.js 3.0
* ide: vscode
* mysql 8.0
* npm install express cors body-parser connect-multiparty
*/
// 数据库连接配置
const db ={
  mysql:{
      host:'localhost',//主机ip地址
      port:'3306', //端口号
      user:'root', //用户名
      password:'geovindu', //密码
      database:'geovindu', //数据库名
  }
}

  //exports.conMysql = conMysql
  export default db; 
  

  

 

/**
 * sqlMap.js  sql语句
 * node 20 vue.js 3.0
 * ide: vscode
 * mysql 8.0
 * npm install express cors body-parser connect-multiparty
 */
const sqlMap = {
    Stu: {
      add: 'insert into vuedustu(stuId,stuname,stusex,stucollege,stuclass) values (0,?,?,?,?)',
      show: 'select * from vuedustu',
      del: 'delete from vuedustu where stuId = ?',
      update: 'update vuedustu set stuname = ?,stusex = ?,stucollege = ?,stuclass = ? where stuId = ?'
    }
  }
  
// module.exports = sqlMap
export default sqlMap; 

  

/**
* api/StuApi.js
* node 20 vue.js 3.0
* ide: vscode
* mysql 8.0
* npm install express cors body-parser connect-multiparty
*/

import sqlMap from '../sqlMap.js'
import db from '../db.js'
import  express from 'express'
const router = express.Router()
import  mysql from 'mysql'
const $sql =sqlMap// require('../sqlMap')

// 连接数据库
const conn = mysql.createConnection(db.mysql)
conn.connect()
const jsonWrite = function (res, ret) {
  if (typeof ret === 'undefined') {
    res.json({
      code: '1', msg: '操作失败'
    })
  } else {
    res.json(
      ret
    )
  }
}
// 接口:增加信息
router.post('/addStu', (req, res) => {
  const sql = $sql.Stu.add
  const params = req.body
  console.log('添加', params)
  conn.query(sql, [params.stuname, params.stusex, params.stucollege, params.stuclass], function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

// 接口:查询全部  http://localhost:3003/Stu/showStu
router.get('/showStu', (req, res) => {
  const sql = $sql.Stu.show
  const params = req.body
  console.log(params)
  conn.query(sql, [params.stuId, params.stuname, params.stusex, params.stucollege, params.stuclass], function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

// 接口:删除信息
router.post('/delStu', (req, res) => {
  const sql = $sql.Stu.del
  const params = req.body
  console.log('删除', params)
  conn.query(sql, [params.stuId], function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

// 接口:修改信息
router.post('/updateStu', (req, res) => {
  const sql = $sql.Stu.update
  const params = req.body
  console.log('修改', params)
  conn.query(sql, [params.stu_name, params.stu_sex, params.stu_college, params.stu_class, params.stu_Id], function (err, result) {
    if (err) {
      console.log(err)
    }
    if (result) {
      jsonWrite(res, result)
    }
  })
})

//module.exports = router
export default router //

  

/**
 * index.js  
 * node 20 vue.js 3.0
 * ide: vscode
 * mysql 8.0
 * npm install express cors body-parser connect-multiparty
 */
import  express from 'express'
import  bodyParser from 'body-parser'
import  fs  from 'fs'
import  path  from 'path'
import StuApi from './api/StuApi.js'

const app = express()
//采用设置所有均可访问的方法解决跨域问题
app.all("*", function (req, res, next) {
    //设置允许跨域的域名,*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type");
    //跨域允许的请求方式
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200); //让options尝试请求快速结束
    else
        next();
})
app.use(bodyParser.json()) // 以json格式返回出去
app.use(bodyParser.urlencoded({ extended: false }))


// 后端api路由

app.use('/Stu',StuApi) //
// 监听端口
app.listen(3003)
console.log('success listen at port:3003......')

  

运行:

node index

  

 

posted @ 2024-08-07 20:17  ®Geovin Du Dream Park™  阅读(4)  评论(0编辑  收藏  举报