javascript - Node JS 重定向循环

标签 javascript node.js express redirect routes

我有这段代码:

app.get('/*', function(req, res, next) {
    res.redirect(301, 'http://example.com' + req.path);
});

chrome 写入错误:

“example.com 页面无法正常工作

example.com 重定向您的次数过多。”

有什么想法吗?

最佳答案

您的代码总是会重定向您。

您需要添加主机检查器条件。

例如:

app.get('/*', function(req, res, next) {
   if (/^www\./.test(req.headers.host)) {
     res.redirect(301, 'http://example.com' + req.path);
   }
});

关于javascript - Node JS 重定向循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169542/

相关文章:

javascript - highcharts - 如何按总计对 xAxis 类别进行排序?

javascript - 下拉选择导致自动水平滚动

javascript - 运行 npm start 时启动脚本丢失错误

javascript - 使用 React 和 Axios 从 Express API 下载文件

javascript - 通过 Google Sheets Apps 脚本访问 Telegram Bot API 文件

javascript - 在div中查找文本被剪切的位置

node.js - Mocha JS : How to share assertion/"it(' should')"code between tests

javascript - 在 Js 文件中运行 npm start

javascript - Node.js 版本已更改,脚本不再工作

javascript - findOneAndUpdate mongoDB 未正确返回