当前位置: 移动技术网 > IT编程>开发语言>JavaScript > [Node.js] mySQL数据库 -- 英雄英雄管理系统接口

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

2020年07月09日  | 移动技术网IT编程  | 我要评论

新增接口

// 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: "查询失败",
            });
        }
    });
});

本文地址:https://blog.csdn.net/DeathDomain/article/details/107166018

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网