mongoose---分页查询
app.js
const express = require('express'); const app = express(); const fs = require('fs') const path = require('path') const favicon = require('serve-favicon'); const bodyParser = require('body-parser') const multiparty = require('multiparty'); const cookieParser = require('cookie-parser') const session = require('express-session'); app.post('/getArticals',function(req,res){ const form = new multiparty.Form(); const Item = require('./models/articals.js'); form.parse(req , function(err,fields,files){ var page = parseInt(fields.page); var pageSize = parseInt(fields.pageSize); var query = Item.find({}); query.skip((page-1)*pageSize); query.limit(pageSize); query.exec(function(err,rs){ if(err) return next(err); console.log(rs); res.json(rs); }) }) }) app.listen(3000);
articals.js
var mongoose = require('mongoose'); //mongoose.connect('mongodb://localhost/hangaoke'); try { mongoose.connect('mongodb://localhost/hangaoke'); //- starting a db connection }catch(err) { mongoose.createConnection('mongodb://localhost/hangaoke'); //- starting another db connection } var schema = new mongoose.Schema({ title:String, content:String, },{ versionKey: false // You should be aware of the outcome after set to false }) module.exports = mongoose.model('articals' , schema);
相关推荐
80530895 2020-07-05
mkhhxxttxs 2020-06-14
80500495 2020-06-14
86211943 2020-03-01
80530895 2020-02-23
80500495 2020-01-29
86211943 2020-01-24
lovecodeblog 2020-01-24
87261046 2019-12-23
86211943 2019-12-20
lightlanguage 2019-12-16
MYRENZHIBO 2019-08-28
85234656 2018-09-03
fudirong 2012-04-12
80500495 2019-07-01