修改Node(express)服务器中的正文后,是否可以重定向post请求?

lb3vh1jj  于 2023-01-01  发布在  Node.js
关注(0)|答案(1)|浏览(142)

我知道可以将post请求重定向到另一个url,如下所示:

app.post((req, res) => {
   res.redirect(307, 'https://something.com/api/test');
})

this will send the body also to something.com/api/test. What I want to do is:

app.post((req, res) => {
  req.body.something = 'somevalue'; // this value is not added.
  res.redirect(307, 'https://something.com/api/test');
})

是否可以修改正文并将其转发到重定向url?
谢谢你的帮助。

6tdlim6h

6tdlim6h1#

//Use query instead of body
const url = require('url')
app.post('/',(req,res)=>
{
    res.redirect(url.format({
        pathname:'/newUrl',
        query: {
           "email": req.body.email //assigning a value from the body
           "theMissingValue" : "some-value" //manually assigning a value
         }
      }));
})

app.get('/newUrl',(req,res)=>
{
    console.log(req.query.theMissingValue); //prints "some-value"
})

相关问题