Expressjs分页重定向

时间:2016-06-25 18:49:13

标签: node.js redirect express pagination

我有一个分页设置,其中我的Feed显示索引路径路径/,然后使用/feed/:pageNumber访问其他页面。我对提供下一个和上一个记录的分页没有任何问题,但是当我想在上一个上一页上有能力时,单击以重定向到/路径,因为它具有最新记录。我尝试在else语句中使用res.redirect(' /'),但收到错误Error: Can't set headers after they are sent.。有没有更好的方法在上一次上一页点击时重定向到家?

E.x。用户点击' /',点击下一步,然后发送到' / feed / 2'。当用户点击上一个时,他们应该被带回' /'。如果它们在/ feed / 3,4,5等等/那么它将使用户比当前参数值小一个。

第I部分试图修复:

if(req.params.pageNumber > 2){
                    res.locals.previous = true;
                    res.locals.previousPage = req.params.pageNumber - 1;
                } else {
                    res.locals.previous = true;
                    res.locals.previous = res.redirect('/');
                }

查看:

<!DOCTYPE html>
<head>
    {{> app/app-head}}
</head>
<body>
    {{> app/app-navigation}}

    <div class="container"> 
        <h1 class="page-title-header">Activity Feed</h1>
            {{> app/card}}
    </div>
    {{#if previous}}
    <a href="/feed/{{previousPage}}">Previous Page</a>
    {{/if}}
    {{#if secondPage}}
    <a href="/feed/{{secondPage}}">Next Page</a>
    {{/if}}
    {{#if next}}
    <a href="/feed/{{nextPage}}">Third Page</a>
    {{/if}}
</body>

路线:

/*====   /  ====*/

appRoutes.route('/') 

    .get(function(req, res){

        models.Card.findAll({
            order: 'cardDate DESC',
            include: [{
                model: models.User,
                where: { organizationId: req.user.organizationId },
                attributes: ['organizationId', 'userId']
            }],
            limit: 10
        }).then(function(card){

            function feedLength(count){
                if (count >= 10){
                    return 2;
                } else {
                    return null;
                }
            };

            res.render('pages/app/high-level-activity-feed.hbs',{
                card: card,
                user: req.user,
                secondPage: feedLength(card.length)
            });
        });
    })

    .post(function(req, res){
        models.Card.create({
            card: req.body.cardDate,
            userId: req.user.userId
        }).then(function() { 
            res.redirect('/app');
        }).catch(function(error){
            res.send(error);
        })
    });

appRoutes.route('/feed/:pageNumber')

    .get(function(req, res){

        function paginationPage(count){
            if(count == 2){
                return 10;
            } else {
                return (count - 1) * 10;
            }
        };
        var skip = parseInt(req.params.pageNumber);

        models.Card.findAll({
            order: 'cardDate DESC',
            include: [{
                model: models.User,
                where: { organizationId: req.user.organizationId },
                attributes: ['organizationId', 'userId']
            }],
            offset: paginationPage(skip),
            limit: 10
        }).then(function(annotation){
                if(annotation.length == 10){

                    res.locals.next = true;
                    res.locals.nextPage = parseInt(req.params.pageNumber) + 1;
                    console.log('This is the next page pagination: ' + res.locals.nextPage);
                }

                if(req.params.pageNumber > 2){
                    res.locals.previous = true;
                    res.locals.previousPage = req.params.pageNumber - 1;
                } else {
                    res.locals.previous = true;
                    res.locals.previous = res.redirect('/');
                }
            res.render('pages/app/high-level-activity-feed.hbs',{
                card: card,
                user: req.user
            });
        });
    })

1 个答案:

答案 0 :(得分:1)

问题是双重的:

  • 您的模板始终在上一页链接前加上/feed/

    <a href="/feed/{{previousPage}}">Previous Page</a>
    
  • res.redirect('/')在您的Express服务器中执行实际重定向(它不会生成链接或客户端javascript)

一种可能的解决方案是在模板中添加第三种状态:

{{#if backHome}}
<a href="/">Previous Page</a>
{{/if}}

在您的服务器代码中:

if (req.params.pageNumber > 2) {
  res.locals.previous = true;
  res.locals.previousPage = req.params.pageNumber - 1;
} else {
  res.locals.backHome = true;
}
相关问题