美文网首页
2023-08-15 uniapp +expressgenera

2023-08-15 uniapp +expressgenera

作者: swp小小喀嚓鱼 | 来源:发表于2023-08-14 15:28 被阅读0次
onLoad() {
            uni.request({
                //localhost也可以换成本地局域网ip地址,192.168.xxxx
                url: 'http://localhost:3000/getlist',
                success: (res) => {
                    // console.log(res);
                    // console.log(res.data);
                    this.xxdata = res.data
                }
            })
        },

全局安装 express和express-generator
在某处文件夹,打开命令窗口,输入express myxxx
此时会自动创建很多文件,
cd myxxx
npm install
npm start
在浏览器输入http://localhost:3000,后端服务就启动了
继续安装nodedemo,下载成功后,在package.json中新增一行    "start1": "nodemon ./bin/www"
目的是为了修改代码的时候,不用反复重启了,运行也变成了npm start1
自行配置了sql,创建db文件夹,在内部创建sql.js
const mysql = require('mysql');
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    port: 3307,
    database: 'demo1'

})
module.exports = connection

[图片上传失败...(image-aa822d-1692084160557)]

改造routes中的index.js,使得数据是数据库获取的
var express = require('express');
var router = express.Router();
var connction = require('../db/sql.js')

// 解决跨域问题
router.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OP0TIONS");
  res.header("X-Powered-By", "Express");
  res.header("Content-Type", "text/html; charset=UTF-8");
  next();
});

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', {
    title: 'Express1xx'
  });
});



router.get('/list', function (req, res, next) {
  res.send({
    "code": 1,
    data: [{
      id: 1,
      name: 'html'
    },
    {
      id: 2,
      name: 'css'
    },
    {
      id: 3,
      name: 'javascript'
    },
    ]
  })
});



router.get('/getlist', function (req, res, next) {
  connction.query('select * from users', function (error, results, fields) {
    if (error) {
      result = {
        warn: 'err',
        message: '获取数据库时发生搓搓'
      }
      res.send(result)
    } else {
      res.send(results)
    }
  })
});

module.exports = router;

相关文章

网友评论

      本文标题:2023-08-15 uniapp +expressgenera

      本文链接:https://www.haomeiwen.com/subject/zxlcmdtx.html