nodejs编写查询某个图书(单条数据)接口
nodejs编写查询某个图书(单条数据)接口
// 编写 查询某个图书(单条数据)接口 /book/id // http://localhost:4000/book/12 app.get('/book/:id', function(req,res){ //获取 客户端 传递过来的 参数id var id = req.params.id; // console.log('传递过来的id是:'+id); // sql语句 var sql = `select * from book where bid=${id}`; // 执行sql语句 mysql.query(sql, function(error,data){ if(error){ return '' }else{ // 查询成功 // 输出json格式的数据 res.json({ status:200, msg:'查询成功', data:data }) } }) })
通过postman接口测试工具,测试如下所示:
