【问题标题】:After post saved to database, how do I route to another ejs page将帖子保存到数据库后,如何路由到另一个 ejs 页面
【发布时间】:2019-05-18 05:16:17
【问题描述】:

如何使用 ejs 在 node.js 中发布帖子后加载另一个页面。

//router
function router() {
   ticketRouter.route("/create/submit") // tied to submit from one ejs
      .post(dataControllerThatTakesCareOfThis) // works fine

    // either in here, or even the controller I suppose I want to load another page after the submit takes place?
     res.redirect(200, '/load:postSubmit') // doesn't work
     res.send({ redirect: '/load:postSubmit' }) // great for ajax response only

}

module.exports = router;

那么,您可以在 node.js 中执行上述操作,而无需返回给客户端一些信息,然后让其提出问题以进行处理吗?是否有“重定向”或这样路由响应的方法?

谢谢

【问题讨论】:

  • 你在这里写的东西让我很困惑。您正在调用“res.redirect”和“res.send”,但您不在快速中间件或 http 回调中。你真的展示了完整正确的代码吗?
  • 不,这不是完整的代码,而是我正在尝试的示例。一般来说,我只是在询问帖子的处理程序(路由,控制器)是否可以转发到另一个路由处理程序,而不是像 ajax 一样将响应一直发送回客户端,这样就可以转身请求另一个页面.抱歉,如果不清楚。

标签: node.js ajax express redirect ejs


【解决方案1】:

假设您使用的是 ExpressJS,而 EJS 是您的模板引擎:

您应该处理您的重定向到 dataControllerThatTakesCareOfThis 函数。

这两行:

// either in here, or even the controller I suppose I want to load another page after the submit takes place?
 res.redirect(200, '/load:postSubmit') // doesn't work
 res.send({ redirect: '/load:postSubmit' }) // great for ajax response only

不起作用,因为它们使用了 res 对象,该对象未在此处定义,而是在 Router 对象的 Post 方法的回调中。

更多信息在express documentation

【讨论】:

  • 有趣。所以该功能是路由器可以利用的控制器功能,我已经尝试 res.redirect(200, url) 到那里,但它似乎打印出来 OK,发布到:/load:postSubmit 作为文本响应
  • 这就是答案,因为我怀疑这种方法是错误的。我需要链接中间件回调并使用 next()。谢谢
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
相关资源
最近更新 更多