首页 > 解决方案 > Express-替代重定向

问题描述

所以我想知道 Express 中是否有一种方法可以为用户加载特定页面,而无需将用户重定向到 html 文件。

所以而不是res.redirect("https://mywebsite.com/subsite");

也许还有另一种方法来加载不会触发的子站点“ subsite.html”

app.get(/\/subsite(?:\.html)?$/, function(){});

事件。

所以我想捕捉所有调用/subsite/subsite.html检查是否允许浏览器/客户端进入该站点。如果一切正确,我会让浏览器进入网站,否则我想实现重定向。

到目前为止我得到了什么:

app.get(/\/subsite(?:\.html)?$/, function(req, res) {
  let isValid = checkIfUserIsValid() /* my own logic */
  if (isValid) {
   //how to load subsite.html on the client?
  } else res.redirect("https://mywebsite.com/subsite");
});

注意:我需要一种方法,因为我陷入了重定向循环。

标签: javascriptnode.jsexpress

解决方案


app.get(“/xx“, (req, res) => {
    let permissionsOk = // check your user's permissions
    if (permissionsOk) {
        res.send(/* regular page html */);
    } else {
        res.send(/* access denied html */);
    }
});

这个想法是你可以有条件响应并根据一些内部逻辑在同一条路线上提供不同的html


推荐阅读