我正在使用Node.js和Express开发一个webapp。在这个应用程序中,我管理一个JSON数据流,使用Tumblr API创建一个博客阅读器。
我把路线写成:
app.get('/blog', function(req, res) {
var Tumblr = require('tumblr').Tumblr
, keys = require('./keystore');
// Instancing Tumblr object to connect with a certain blog.
var blog = new Tumblr('myblog.tumblr.com', keys.tumblrConsumerSecret);
// Using Tumblr API to retrieve posts from a remote blog.
// For more info about methods and parameters to use, read the
// documentation for Tumblr's API v2.
blog.posts({offset: 0, limit: 5}, function(err, tumblr) {
if (err) {
res.send('Error page... I suppose!');
}
else {
res.render('blog',
{
blog: tumblr.posts
});
}
});
});
blog.jade
:
each post in blog
h2 #{post.title}
.justified
!= post.body
现在,我想将编号页面导航功能(作为服务器端)添加到阅读器。
page 1: posts from 1 to 5
page 2: posts from 6 to 10
(etc...)
我能做什么?
最好的问候,Vi。
答案 0 :(得分:2)
按照chovy的建议,我找到了解决方案。
在app.js
:
function checkParamPage(req, res, next) {
if (req.params.page > 0) {
next();
}
else { res.redirect('/error'); }
}
app.get('/blog/:page', checkParamPage, routes.blog);
app.get('/error', function(req, res) { res.send('Error!'); })
在routes/index.js
:
exports.blog = function(req, res) {
var _req = req;
var _res = res;
var Tumblr = require('tumblr').Tumblr
, keys = require('../keystore')
, async = require('async');
var page = req.params.page
, limit = 5
, offset = page*limit - limit;
// Instancing Tumblr object to connect with a certain blog.
var blog = new Tumblr('myblog.tumblr.com', keys.tumblrConsumerSecret);
async.parallel([
function(callback){
blog.info(function(err, res) {
if (err) {
callback('error', null);
} else {
callback(null, Math.ceil(res.blog.posts/limit));
}
});
},
function(callback){
blog.posts({offset: offset, limit: limit}, function(err, res) {
if (err) {
callback('error', null);
} else {
if (tumblr.posts.length === 0) {
callback('empty', null);
} else {
callback(null, res.posts);
}
}
});
}
], function(err, res) {
if (err) {
_res.redirect('/error');
} else if (page > res[0]) {
_res.redirect('/error');
} else {
_res.render('blog',{
current: '/blog',
title: 'MyTitle',
pages: {
all: res[0],
current: page
},
posts: res[1]
});
}
}
);
};
然后,blog.jade
:
extends layout
block content
// Including sidebar.
include partials/sidebar
div.span8
h1 Blog
hr
each post in posts
h2 #{post.title}
div#tumblr-post.justified
!= post.body
hr
.pagination.pagination-centered
ul
if (pages.current > 1)
li
a(href='/blog/1') First
else
li.disabled
a(href='/blog/1') First
- for (var i = 1; i <= pages.all; i++)
if (i == pages.current)
li.active
a(href='/blog/' + i) #{i}
else
li
a(href='/blog/' + i) #{i}
if (pages.current < pages.all)
li
a(href='/blog/' + pages.all) Last
else
li.disabled
a(href='#') Last
// Including footer.
include partials/footer
这就是全部(并且工作正常)!
答案 1 :(得分:1)
通常你会创建分页链接:
/blog/pg1
/blog/pg2
/blog/pg3
您可以获得如下页码:
app.get('/blog/pg:pg', function(req, res){
var pg = req.params.pg,
offset = pg*5-5;
blog.posts({offset: offset, limit: 5}, ...);
});