import express from "express";
import mongodb from "mongodb";
const mongo = mongodb.MongoClient;
const app = express();
//主页路由设置;
//前端路由;
app.get("/",(req,res)=>{
res.sendFile(__dirname+"/views/index.html");
})
//接口路由;
app.get("/pagination",(req,res)=>{
// 查看前端发送的 page 字段的数据;
// console.log(req.query.page);
// 连接数据库;获取数据;返回数据;
// res.send("[1,2,3,4,5]");
// console.log(mongo);
//断定前端是否传入数据; 若是值为空那么赋值为0;
let page = req.query.page;
page = page ? parseInt(page) : 0 ;
//连接数据库并返回数据;
mongo.connect("mongodb://localhost:27017",(err,client)=>{
let odb = client.db("hello");
//设计分页逻辑 => skip | limit 来执行; skip 上的page值 => 前端发送的内容;
odb.collection("movie")
.find({})
.skip(page * 5)
.limit(5)
.toArray((err,result)=>{
let oResult = Object.assign({},{
data:result
});
res.send(oResult);
})
});
})
//端口监听;
app.listen("3000","localhost",()=>{
console.log("服务开启成功,请访问 http://localhost:3000");
})