javascript - 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");
});
注意:我需要一种方法,因为我陷入了重定向循环。
解决方案
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
推荐阅读
- java - 更新哈希映射格式的哈希映射中的对象变量
> - types - 为什么在循环后迭代字符串不执行代码?
- xslt - 通过 XSLT 将 XML 转为文本
- java - 串行链接错误和错误路径 jre 1.7.0_15 32-bi
- html - 从 hta 按钮启动 plink-ssh 连接并将值传递给 ssh 命令
- android - 我应该在 onResume() 调用中从数据库中检索记录吗?
- jenkins-pipeline - Jenkins Scripted Pipeline: sshCommand 执行 statusCode
- php - 无法找到我的 iOS 设备 ID 并连接到 APNS 服务器以在 PHP 中进行推送通知
- swift - 如何在 swift (FCM) 中接收数据通知
- javascript - 如何修复“http://localhost:3000”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。