如何将post请求重定向到node.js中的其他url?

时间:2018-02-03 05:38:16

标签: javascript node.js express

我正在尝试使用请求1中的其他参数将发布请求从一台服务器重定向到另一台服务器。您可以查看以下代码。

:se cm=blowfish2

如何将额外字段传递给其他请求?

2 个答案:

答案 0 :(得分:1)

307响应表示应该使用相同的方法将请求重复到新URL,但是您无法更改浏览器将发送的请求正文。

如果您可以使用GET,则可以更改参数(因为它们是请求路径的一部分)。

您可以通过从服务器端新发送到新服务器来代理请求,并将响应传递回客户端。如果你这样做,你就可以随心所欲地改变身体。

另一种选择是使用HTML页面来回复隐藏表单字段中的已发布数据,以及要添加的字段,并让用户单击提交按钮。您将表单的操作指向其他服务器。

答案 1 :(得分:0)

我可以对我的代码执行此操作,并且所有工作都只需添加新参数并在那里解析

app.use(bodyParser.urlencoded({ extended: true }));

app.post('/pay1',(req,res)=>{
    console.log(req._body);
    console.log(req.body); // output {'a':'value'}
    req.body['new']='other';
    console.log(req._body);
    console.log(req.body); // {'a':'value','new':"other"}
    res.redirect(307,'/pay2?a=value');
});

app.post('/pay2',(req,res)=>{
// this request will be in other server, for now I am testing in same server
    console.log(req.query.a + req.query.whatever //Whatever you have in the first request too); // output {'a':'value'}
    res.send('2dsaf');
});
相关问题