node.js 在 express 如何将用户重定向到外部 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28352871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In express how do I redirect a user to an external url?
提问by Michael Joseph Aubry
I have a payment system using node.js and braintree, when the payment is successful I want to send the user to the back end. My back end is setup elsewhere.
我有一个使用 node.js 和 Braintree 的支付系统,当支付成功时,我想将用户发送到后端。我的后端在别处设置。
I have tried
我试过了
res.writeHead(301,
{Location: 'http://app.example.io'}
);
res.end();
So window.location is obviously not available. I cant think of any ways to redirect a user?
所以 window.location 显然不可用。我想不出任何重定向用户的方法?
回答by TheIronDeveloper
You can do
你可以做
res.redirect('https://app.example.io');
Express docs: https://expressjs.com/en/api.html#res.redirect
回答by AIon
The selected answer did not work for me. It was redirecting me to: locahost:8080/www.google.com- which is nonsense.
所选答案对我不起作用。它将我重定向到:locahost:8080/www.google.com- 这是胡说八道。
301 Moved Permanentlyneeds to be included with res.status(301)as seen below.
301 Moved Permanently需要包括在内,res.status(301)如下所示。
app.get("/where", (req, res) => {
res.status(301).redirect("https://www.google.com")
})
You are in the same situation since your back-end is elsewhere.
您处于相同的情况,因为您的后端在其他地方。
回答by Messivert
app.get("/where", (req, res) => {
res.status(301).redirect("https://www.google.com")
})
You need to include the status (301)
您需要包括状态 (301)
回答by Granit
I just have the same issue and got it work by adding "next". I use routers so maybe you have same issue as mine? Without next, i got error about no render engine...weird
我只是遇到了同样的问题,并通过添加“下一步”来解决问题。我使用路由器,所以也许你有和我一样的问题?没有下一个,我得到关于没有渲染引擎的错误......奇怪
var express = require('express');
var router = express.Router();
var debug = require('debug')('node_blog:server');
/* GET home page. */
router.get('/', function(req, res, next) {
debug("index debug");
res.render('index.html', { title: 'Express' });
});
router.post("/", function (req, res, next) {
//var pass = req.body("password");
//var loginx = req.body("login");
//res.render('index.html', { title: 'Express' });
res.redirect("/users")
next
});
module.exports = router;
回答by user2056154
None of these worked for me, so I tricked the receiving client with the following result:
这些都不适合我,所以我用以下结果欺骗了接收客户端:
res.status(200).send('<script>window.location.href="https://your external ref"</script>');
Some will say if noscript is on this does not work, but really which site does not use it.
有些人会说如果 noscript 在这上面不起作用,但实际上哪个站点不使用它。

