app.get('/new/page/:page/limit/:limit', function (req, res) { var page = parseInt(req.params.page), limit = parseInt(req.params.limit); var result = db; try{ result = result.sort(naturalCompare.bind(result,"update_date")); }catch(e){ result = result.sort(function(a,b){ return a["update_date"] < b["update_date"] }) } result = r.limit(result,page*limit,limit); res.header('Content-Type', 'application/json'); res.send(JSON.stringify(result)); });
app.get('/popular/page/:page(\\d+)/limit/:limit', function (req, res) { var page = parseInt(req.params.page), limit = parseInt(req.params.limit); var result = db; try{ result = result.sort(naturalCompare.bind(result,"popularity")); }catch(e){ result = result.sort(function(a,b){ return a["popularity"] < b["popularity"] }) } console.log(page*limit, (page+1)*limit); result = r.limit(result,page*limit,limit); res.header('Content-Type', 'application/json'); res.send(JSON.stringify(result)); });
app.get('/:query/limit/:limit/page/:page', function (req, res) { var result = new Set(), page = parseInt(req.params.page), limit = parseInt(req.params.limit), query = req.params.query; var result = sift({ $where: function(){ if(this.name.search(new RegExp(query,"im")) > -1 ) return true; if(this.readme && this.readme.search(new RegExp(query,"im")) > -1) return true; if(this.author.search(new RegExp(query,"im")) > -1 ) return true; if(this.keywords && this.keywords.find(function(v){ return query.indexOf(v) > -1 })) return true; } }, db); result = r.limit(result,page*limit,limit); res.setHeader('Content-Type', 'application/json'); res.send(JSON.stringify(result)); });
app.get('/:query/order/:order/limit/:limit/page/:page', function (req, res) { var result = new Set(), page = parseInt(req.params.page), limit = parseInt(req.params.limit), query = req.params.query, order = req.params.order; var result = sift({ $where: function(){ if(this.name.search(new RegExp(query,"im")) > -1 ) return true; if(this.readme && this.readme.search(new RegExp(query,"im")) > -1) return true; if(this.author.search(new RegExp(query,"im")) > -1 ) return true; if(this.keywords && this.keywords.find(function(v){ return query.indexOf(v) > -1 })) return true; } }, db); try{ result = result.sort(naturalCompare.bind(result,order)); }catch(e){ result = result.sort(function(a,b){ return a[order] < b[order] }) } result = r.limit(result,page*limit,limit); res.header('Content-Type', 'application/json'); res.send(JSON.stringify(result)); });