In express how do I redirect a user to an external url?

后端 未结 6 2167
甜味超标
甜味超标 2020-12-05 06:28

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

相关标签:
6条回答
  • 2020-12-05 06:55

    You can do

    res.redirect('https://app.example.io');
    

    Express docs: https://expressjs.com/en/api.html#res.redirect

    0 讨论(0)
  • 2020-12-05 07:00

    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.

    0 讨论(0)
  • 2020-12-05 07:01

    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.

    0 讨论(0)
  • 2020-12-05 07:02
        app.get("/where", (req, res) => {
    
        res.status(301).redirect("https://www.google.com")
    
    })
    

    You need to include the status (301)

    0 讨论(0)
  • 2020-12-05 07:09

    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;
    
    0 讨论(0)
  • 2020-12-05 07:09

    redirect a user to an external URL is pretty simple.

    res.redirect('enter the URL here');
    
    0 讨论(0)
提交回复
热议问题