在表达如何将用户重定向到外部URL?

Mic*_*bry 32 routes node.js express

我有一个使用node.js和braintree的支付系统,当支付成功时,我想将用户发送到后端.我的后端设置在其他地方.

我试过了

res.writeHead(301,
  {Location: 'http://app.example.io'}
);
res.end();
Run Code Online (Sandbox Code Playgroud)

所以window.location显然不可用.我想不出任何重定向用户的方法?

The*_*per 88

你可以做

res.redirect('https://app.example.io');
Run Code Online (Sandbox Code Playgroud)

快递文档:https://expressjs.com/en/api.html#res.redirect

  • 它重定向到`/` (4认同)
  • 我尝试了`res.redirect(301,'http://app.example.io');`仍然没有用。嗯? (2认同)

AIo*_*Ion 9

选定的答案对我不起作用.它将我重定向到:locahost:8080/www.google.com- 这是无稽之谈.

301 Moved Permanently需要包括在内,res.status(301)如下所示.

app.get("/where", (req, res) => {

    res.status(301).redirect("https://www.google.com")

})
Run Code Online (Sandbox Code Playgroud)

由于你的后端在其他地方,你处于相同的情况.

  • 我遇到了类似的问题,我发现这与需要附加到重定向网址的`https://`或`http://`有关.如果不是,它似乎失败了.同时添加"301"会导致重定向URL被浏览器缓存,这意味着每当您点击相同的URL时,它都会将您重定向到第一个地址,无论它是否可以更改. (3认同)

小智 5

    app.get("/where", (req, res) => {

    res.status(301).redirect("https://www.google.com")

})
Run Code Online (Sandbox Code Playgroud)

您需要包括状态(301)