ExpressJS中的重定向,发送和提取参数

问题描述 投票:0回答:1

我有一个在http://localhost:8080上运行的React应用程序。我正在http://localhost:8081

上运行另一个Express应用程序

快速应用程序必须重定向以响应应用程序以及POST参数。我可以通过以下代码重定向到其他URL

var express = require("express");
var router = express.Router();

router.post("/", function(req, res) {
  res.redirect("http://localhost:8080/");
});

module.exports = router;

但是,我无法弄清楚如何在React端传递参数以及读取参数。任何帮助都将受到高度赞赏。预先感谢。

node.js reactjs express
1个回答
0
投票

在服务器端:

 res.redirect("http://localhost:8080/?param1=value1&param2=value2");

在客户端:

function parseQueryParams() {
    const search = window.document.location.search; 
    const out = {};
    if (search) {
        search.substr(1).split('&').forEach((parts) => {
            const pair = parts.split('=');
            out[pair[0]] = decodeURIComponent(pair[1]);
        });
    }
    return out;
}

const params = getQueryParams();
© www.soinside.com 2019 - 2024. All rights reserved.