这两天学习了nodejs mysql 实现分页,很重要,所以,今天添加一点小笔记。
代码如下
var express = require('express'); var router = express.Router(); var settings = require('../settings.js'); var mysql = require('mysql2'); router.get('/', function(req, res, next) { var current_page = 1; //默认为1 var num = 9; //一页条数 if (req.query.page) { current_page = parseInt(req.query.page); } var last_page = current_page - 1; if (current_page <= 1) { last_page = 1; } var next_page = current_page + 1; var str = 'SELECT left(paragraph,50) as paragraph,date,id FROM notice limit ' + num + ' offset ' + num * (current_page - 1); var conn = mysql.createConnection(settings.db); conn.connect(); conn.query(str, function(err, rows, fields) { if (err) { req.flash('error', '数据查询有误'); } if (!err) { if (!rows[0]) { req.flash('error', '已到最后一页,请返回'); } res.render('notice', { last_page: last_page, next_page: next_page, current_page: current_page, mes: rows, error: req.flash('error').toString() }); } }); conn.end(); }); module.exports = router;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。