📖Vue+node.js 简单获取Mysql 数据库的数据

前言

我的数据库是利用php study在本地搭建的

本地环境已经安装node.js 与 nodejs 中的mysql,http 扩展

Vue代码

  1. <template>
  2. <div id="Nodejs">
  3. <ul>
  4. <li v-for='nodetxt in textAry'>{{nodetxt.name}}</li>
  5. </ul>
  6. <button @click='sqlUP'>获取数据库信息</button>
  7. </div>
  8. </template>
  9. <script>
  10. const axios = require('axios');
  11. export default{
  12. name:"Nodejs",
  13. data(){
  14. return {
  15. NodeApi:'/user',
  16. textAry:[]
  17. };
  18. },methods:{
  19. sqlUP:function(){
  20. this.$axios.get('/Node'+this.NodeApi)
  21. .then(res => (
  22. this.textAry = res.data.data
  23. ))
  24. .catch(function(error){console.log('获取失败');});
  25. }
  26. }
  27. }
  28. </script>
  29. <style scoped>
  30. .table th{text-align: center;}
  31. </style>

Node.js 代码

  1. const express = require('express')
  2. const mysql = require('mysql')
  3. const cors = require('cors') // 跨域
  4. const bodyParser = require('body-parser') // 解析参数
  5. const app = express()
  6. const router = express.Router()
  7. //上面是引入各种模块
  8.  
  9.  
  10. app.all('*', function (req, res, next) {
  11. res.header('Access-Control-Allow-Origin', '*'); //设置允许跨域的域名,*代表允许任意域名跨域
  12. res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With');//允许的header类型
  13. res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS'); //跨域允许的请求方式
  14. if (req.method == 'OPTIONS') {
  15. res.send(200);//让options尝试请求快速结束
  16. } else {
  17. next();
  18. }
  19. });
  20.  
  21. let ApiProt = 9999;
  22. //启动项目,端口号为8000
  23. app.listen(ApiProt,()=>console.log('服务启动,请访问 http://localhost:'+ApiProt+'/user'))
  24. //数据库信息,密码一般,默认root,我是修改过
  25. const option = {
  26. host:'localhost',
  27. user:'Nodesql',
  28. password:'Nodesql',
  29. port:'3306',
  30. database:'Nodesql',
  31. connectTimeout:5000, // 连接超时
  32. multipleStatements:false // 是否允许一个query中包含多条sql语句
  33. }
  34. app.use(cors()) //解决跨域
  35. app.use(bodyParser.json()) //json请求
  36. app.use(bodyParser.urlencoded({extended:false})) // 表单请求
  37. //连接数据库
  38. const conn = mysql.createConnection(option)
  39. //all代表所有的请求方式(包括get/post) '/user' 给一个路径(自己定义) req代表发起请求(request) res代表接收请求(response)
  40. app.all('/user',(req,res)=>{
  41. // 选择user表 根据你自己的表名 用json的格式输出
  42. conn.query('SELECT * From websites',(e,r)=>res.json(new Result({data:r})))
  43. })
  44.  
  45. app.all('/user_install',(req,res)=>{
  46. // 选择user表 根据你自己的表名 用json的格式输出
  47. conn.query('SELECT * From websites',(e,r)=>res.json(new Result({data:r})))
  48. })
  49.  
  50. // 输出的内容
  51. function Result({code=1,msg='',data={}}){
  52. this.code = code;
  53. this.msg = msg;
  54. this.data = data
  55. }

跨域

由于不在同一接口所以会遇到跨域问题,我这里的解决方法是使用代理服务器,也就是我之前文章有写的
http-proxy-middleware 本地代理配置

  1. //config/index.js
  2.  
  3. proxyTable: {
  4. '/Node':{//Node.js测试
  5. target:"http://localhost:9999",
  6. changeOrigin:true,
  7. pathRewrite:{
  8. '^/Node':'/'
  9. }
  10. }
  11. },

结尾

Vue+node.js 简单获取Mysql 数据库的数据插图

Vue+node.js 简单获取Mysql 数据库的数据插图1

标签

🧐发表评论

请将下面的 铅笔 放到右边的灰色圆圈中