0

我正在使用 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...)

我能做什么?

最好的问候,六。

4

2 回答 2

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

就是这样(它工作正常)!

于 2012-10-22T11:41:20.247 回答
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}, ...);
});
于 2012-10-21T20:14:07.007 回答