In express how do I redirect a user to an external url?
Solution 1
You can do
res.redirect('https://app.example.io');
Express docs: https://expressjs.com/en/api.html#res.redirect
Solution 2
The selected answer did not work for me. It was redirecting me to: locahost:8080/www.google.com
- which is nonsense.
301 Moved Permanently needs to be included with res.status(301)
as seen below.
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.
Solution 3
app.get("/where", (req, res) => {
res.status(301).redirect("https://www.google.com")
})
You need to include the status (301)
Solution 4
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;
Solution 5
redirect a user to an external URL is pretty simple.
res.redirect('enter the URL here');
Michael Joseph Aubry
Updated on October 15, 2021Comments
-
Michael Joseph Aubry over 2 years
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.
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?
-
Michael Joseph Aubry over 9 yearsI tried that earlier but I dont think it worked trying it again.
-
TheIronDeveloper over 9 yearsI just tested it on a personal app and it redirects correctly. Question: What version of express are you using?
-
TheIronDeveloper over 9 yearsWell if your willing to link your github repo I can try to debug more, but to answer your question... this is how you redirect to an external site in express.
-
Michael Joseph Aubry over 9 yearsYeah I would agree, it's weird, I am sure I am overlooking something. On success I will just render a success message and give the user the option to go to the backend.
-
Y Anderson about 6 yearsI was having similar issues and I found that this has to do with
https://
orhttp://
needing to be attached to the redirect url. If not it seems to fail. Also adding a301
results in the redirect url being cached by the browser which means everytime you hit the same url it will redirect you to the first address regardless of whether it could have changed or not. -
Krish almost 5 yearshow to pass headers to this redirect url?
-
B--rian almost 5 yearsPlease be so kind and elaborate a bit what you mean by tricked.
-
Alex over 4 yearsHeaders can't be passed to redirect
-
TheIronDeveloper over 4 yearsRather than the server returning a 301 to tell the browser to immediately redirect, they are returning a webpage with JavaScript that redirects the url.
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.