[Node.js] mySQL数据库 -- 英雄英雄管理系统接口

新增接口

// 1.写一个新增接口
// 参数:heroName heroSkill,heroIcon(文件),使用muter从前端接收
app.post("/hero/add", upload.single("heroIcon"), (req, res) => {
    // 1.1 接收前端传递过来的参数
    console.log(req.file.filename);//图像名字
    console.log(req.body);//文本参数
    let heroIcon = 'http://127.0.0.1:4399/' + req.file.filename;
    let { heroName, heroSkill } = req.body;

    //1.2 把这传递过来的数据保存到数据库中.
    // 执行sql语句代码
    connection.query(
        `insert into hero(heroName,heroSkill,heroIcon) values('${heroName}','${heroSkill}','${heroIcon}');`, (error, result, fields) => {
            if (error == null) {
                res.send({
                    code: 200,
                    msg: "新增成功",
                    list: { heroName: heroName, heroSkill: heroSkill },
                });
            } else {
                res.send({
                    code: 400,
                    msg: "新增失败",
                    list: { heroName: heroName, heroSkill: heroSkill },
                });
            }
        }
    );
});

 
 

查询所有的英雄接口

//2,写一个查询所有的英雄接口
// 参数:无
app.get("/hero/all", (req, res) => {
    //直接读取数据库表中所有的英雄数据,返回
    //执行sql语句
    connection.query(`select id,heroName,heroSkill,heroIcon from hero where isDelete = false`, (error, results, fields) => {
        if (error == null) {
            console.log(result);

            res.send({
                code: 200,
                msg: "查询成功",
                data: result,
            });
        } else {
            res.send({
                code: 400,
                msg: "查询失败",
            });
        }
    });
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值