节点JS重定向循环

时间:2016-05-11 17:29:29

标签: javascript node.js express redirect routing

我有这段代码:

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

和chrome写入错误:

" example.com页面无效

example.com重定向了你太多次了。"

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的代码始终会重定向您。

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

例如:

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