nodejs之使用express框架连接mysql数据库,返回jsonapi数据

时间:2022-02-25 04:11:37
var express = require('express');
var router = express.Router();
var url = require('url');
var mysql = require('mysql');
//配置模块
var settings = require('../DbConnection/dbconnection');
//连接数据库
var connection = mysql.createConnection(settings.db);
connection.connect();
router.get('/', function (req, res, next) {
var params = url.parse(req.url, true).query;
console.log("id" + params.id);
var sql = "select * from product";
connection.query(sql, function (error, data) {
console.log(error);
if (error) {
var result = {
"status": "500",
"message": "服务器错误"
}
return res.jsonp(result);
}
else{
var result = {
"status": "200",
"message": "success",
data:data
}
return res.jsonp(result);
}
})
});
module.exports = router;
返回数据为

nodejs之使用express框架连接mysql数据库,返回jsonapi数据