简体   繁体   English

如何在Node博客应用程序中保留分页页面?

[英]How to retain pagination page in Node blog app?

I am building a blog site using Express. 我正在使用Express构建博客站点。 It has an edit and delete features. 它具有编辑和删除功能。 I am trying to redirect to the same page of the pagination I left off in the root route upon deleting or editing an existing post. 我正在尝试重定向到删除或编辑现有帖子后在根路由中遗留的分页的同一页面。 As it stands now, if I delete a post on Page 2 for example, it will redirect me back to Page 1. The same thing will happen if I delete a blog post on Page 2. But I want both functionalities to redirect back to Page 2. Any help is appreciated. 现在,例如,如果我删除第2页上的帖子,它将重定向我回到第1页。如果删除第2页上的博客帖子,也会发生同样的事情,但是我希望这两种功能都重定向到Page 1上。 2.任何帮助,我们将不胜感激。

This is the Edit page on the front-end. 这是前端的“编辑”页面。

<form action="/edit/<%=id %>?_method=PUT?page=<%= currentPage %>" method="POST">
  <div class="form-group">
    <label for="postTitle">Title</label>
    <input type="text" name="postTitle" class="form-control" id="postTitle" autocomplete="off" value="<%= title %>">
    <label for="postBody">Post</label>
    <textarea name="postBody" class="form-control" autocomplete="off" rows="8"><%= content %></textarea>
  </div>
    <button type="submit" name="button" class="btn btn-primary">Publish</button>
</form>

Here is the form for the Delete function on the front-end. 这是前端上删除功能的表格。

  <form action="/delete?page=<%= currentPage %>" method="POST">
    <button type="submit" name="delete" value="<%= post._id %>" class="btn btn-outline-danger">DELETE</button>
  </form>

Here is the root, delete and edit routes on the back-end. 这是后端的根,删除和编辑路由。

// Root route that displays all posts stored in database.
app.get("/", (req, res) => {

  let perPage = 3;
  let totalItems;
  let currentPage = parseInt(req.query.page) || 1;

  Post.find()
    .countDocuments()
    .then(count => {

      totalItems = count;

      Post.find()
        .sort({ date: -1 })
        .skip((currentPage - 1) * perPage)
        .limit(perPage)
        .then(posts => {
          console.log(currentPage);
          res.render("home", {
            posts,
            currentPage,
            totalItems
          });

        });
    })
});


// Route for updating specific post.
app.put('/edit/:id', (req, res) => {
  let currentPage = parseInt(req.query.page) || 1;
  const postEdit = {
    title: req.body.postTitle,
    content: req.body.postBody
  };
  Post.findByIdAndUpdate(req.params.id, postEdit, (err, post) => {
    if (err) {
      console.log(err);
    } else {
      res.redirect("/?page=" + currentPage);
    }
  });
});

// Route for deleting specific post.
app.post("/delete", (req, res) => {
  const deletePost = req.body.delete;
  let currentPage = parseInt(req.query.page) || 1;
  Post.findByIdAndRemove(deletePost, (err) => {
    if (err) {
      console.log(err);
    } else {
      res.redirect("/?page=" + currentPage);
    }
  });
});

It is good idea to just send status while hitting API's rather than redirecting from API it self. 最好在碰到API的同时发送状态,而不是直接从API重定向。 I would recommend you to do the same. 我建议您也这样做。 Once you hit the API in your client, you will get http status code like 200(success) or 500(something went wrong while performing action) or what so ever.Depending on http status code do redirections on client side only(if needed). 一旦您在客户端中点击了API,您将获得http状态代码,例如200(成功)或500(执行操作时出了点问题)等。根据http状态代码,仅在客户端进行重定向(如果需要) 。

app.put('/edit/:id', (req, res) => {

 const postEdit = {
  title: req.body.postTitle,
  content: req.body.postBody
 };

Post.findByIdAndUpdate(req.params.id, postEdit, (err, post) => {
   if (err) {
   console.log(err);
    res.status(500).send(err);
   } else {
    res.status(200).send();// send status only 
   }
 });
});

// Route for deleting specific post.
app.post("/delete", (req, res) => {
 const deletePost = req.body.delete;

  Post.findByIdAndRemove(deletePost, (err) => {
  if (err) {
    console.log(err);
    res.status(500).send(err);
  } else {
    res.status(200).send(); // send status only 
  }
 });
});

you can pass some additional data to your /delete route as a query parameter from client Eg. 您可以将一些其他数据作为来自客户端Eg的查询参数传递到/delete路由。

<form action="/delete?page=2" method="POST">

Instead of 2 you can pass the actual page from your backend in model and use it in the view. 您可以从模型的后端传递实际的页面,而不是2 ,然后在视图中使用它。

And then use it in your delete route as let currentPage = parseInt(req.query.page) || 1; 然后在您的delete路由中使用它,就像let currentPage = parseInt(req.query.page) || 1; let currentPage = parseInt(req.query.page) || 1; same as what you are doing in your / route 与您在/路线中所做的相同

app.post("/delete", (req, res) => {
  const deletePost = req.body.delete;
  let currentPage = parseInt(req.query.page) || 1; // you need to get the value here for this route

  Post.findByIdAndRemove(deletePost, (err) => {
    if (err) {
      console.log(err);
    } else {
      res.redirect('/?page=' + currentPage);
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM