var express = require('express');
var router = express.Router();
let lesson = [
{
id: '1',
title: 'react课程',
type: 'react',
imgurl: 'http://pic2.ablesky.cn/content/pic/coursephoto/2018/01/14/292941d6-c4a0-4948-909f-2f934f9d9b31.jpg'
},
{
id: '2',
title: 'vue课程',
type: 'vue',
imgurl: 'http://pic5.ablesky.cn/content/pic/coursephoto/2017/12/04/9c3f633a-86cc-4475-9f16-0a95ef9bb088.png'
},
{
id: '3',
title: 'react课程',
type: 'react',
imgurl: 'http://img2.imgtn.bdimg.com/it/u=1409224514,45938994&fm=27&gp=0.jpg'
},
{
id: '4',
title: 'vue课程',
type: 'vue',
imgurl: 'http://img5.imgtn.bdimg.com/it/u=974328080,785294559&fm=27&gp=0.jpg'
},
{
id: '5',
title: 'react课程',
type: 'react',
imgurl: 'http://img5.imgtn.bdimg.com/it/u=263704529,3807425268&fm=27&gp=0.jpg'
},
{
id: '6',
title: 'vue课程',
type: 'vue',
imgurl: 'http://img0.imgtn.bdimg.com/it/u=2619639360,2642417572&fm=27&gp=0.jpg'
},
{
id: '7',
title: 'react课程',
type: 'react',
imgurl: 'http://img2.imgtn.bdimg.com/it/u=3421657713,305786063&fm=27&gp=0.jpg'
},
{
id: '8',
title: 'vue课程',
type: 'vue',
imgurl: 'http://img0.imgtn.bdimg.com/it/u=1562822542,680120372&fm=27&gp=0.jpg'
},
{
id: '9',
title: 'react课程',
type: 'react',
imgurl: 'http://img1.imgtn.bdimg.com/it/u=460969158,2720773441&fm=27&gp=0.jpg'
},
{
id: '10',
title: 'vue课程',
type: 'vue',
imgurl: 'http://img0.imgtn.bdimg.com/it/u=933401860,3633993042&fm=27&gp=0.jpg'
},
{
id: '11',
title: 'react课程',
type: 'react',
imgurl: 'http://img0.imgtn.bdimg.com/it/u=1621594874,1585448284&fm=27&gp=0.jpg'
},
{
id: '12',
title: 'vue课程',
type: 'vue',
imgurl: 'http://img4.imgtn.bdimg.com/it/u=3059960288,1553393280&fm=27&gp=0.jpg'
},
{
id: '13',
title: 'react课程',
type: 'react',
imgurl: 'http://img3.imgtn.bdimg.com/it/u=296894285,2057887768&fm=27&gp=0.jpg'
},
{
id: '14',
title: 'vue课程',
type: 'vue',
imgurl: 'http://img1.imgtn.bdimg.com/it/u=3718356041,1085602404&fm=27&gp=0.jpg'
},
{
id: '15',
title: 'react课程',
type: 'react',
imgurl: 'http://img2.imgtn.bdimg.com/it/u=2656601577,2546325327&fm=27&gp=0.jpg'
},
{
id: '16',
title: 'vue课程',
type: 'vue',
imgurl: 'http://img2.imgtn.bdimg.com/it/u=3203805412,4179644444&fm=27&gp=0.jpg'
},
]
//获取课程接口
//服务器一共有18条数据。每次取5条;下次从5条偏移5条
//offset偏移量;limit每次取多少条;type课程类型;
router.get('/:offset/:limit/:type', function (req, res, next) {
console.log(req.params)
let lists=[],newArry=[]
let {offset, limit, type} = req.params
//返回全部课程
if(type==='all'){
lists=lesson
}
//过滤数据
else{
lists=lesson.filter((item, index) => {
return item.type===type
})
}
offset=parseInt(offset)
limit=parseInt(limit)
//.slice(start,end):start必需。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推
newArry=lists.slice(offset,offset+limit)
let hasmore=offset+limit > lists.length ? false : true
res.send({
hasmore,
list:newArry
});
});
module.exports = router;
express返回数据实现分页
最新推荐文章于 2022-06-30 09:19:14 发布