ezyk_cn 项目-实现一个简单的分页接口
时间:2021.5.18
借助 mysql 的查询语句,实现分页接口
import { Router } from 'express'
import mysql from 'mysql'
import config from '../../config/mysql'
const router = new Router()
const db = mysql.createConnection(config)
db.connect(err => {
if (err) {
throw err
}
console.log('数据库连接成功!')
})
const queryItems = params => {
const [start, hit] = [+params.start, +params.hit]
return new Promise((resolve, reject) => {
db.query(
// `select * from tb_articles_for_life`,
`select * from tb_articles_for_life LIMIT ${start},${hit};`,
(err, result) => {
if (err) {
throw err
}
resolve(result)
}
)
})
}
const queryCount = params => {
return new Promise((resolve, reject) => {
db.query('select count(*) from tb_articles_for_life', (err, result) => {
if (err) {
throw err
}
resolve(result[0]['count(*)'])
})
})
}
function getMdFromMysql(params, res) {
Promise.all([queryItems(params), queryCount()]).then(([items, total]) => {
res.send({
total,
items
})
})
}
router.post('/getMdFromMysql', function (req, res, next) {
console.log(req.body)
getMdFromMysql(req.body, res)
})
export default router
结果