简体   繁体   English

从post请求发送数据以在nodejs中获取请求

[英]Sending data from post request to get request in nodejs

Hey guys just started with nodejs and express so i came up with the situation where i want to send my data to my GET req from my POST req.Here is the code below for you to understand嘿伙计们刚开始使用 nodejs 和 express 所以我想出了我想从我的 POST req 将我的数据发送到我的 GET req 的情况。这是下面的代码供你理解

app.post('/run-time',(req,res)=>{
    const stoptime=req.body.stop
    const plannedtime=req.body.planned
    const runtime=plannedtime-stoptime
    res.redirect('/run-time')
})

this is my POST req where i fetched the values from the form and then calculated the 'runtime' now then i have to redirect to specific GET route这是我的 POST 请求,我从表单中获取值,然后计算“运行时”,然后我必须重定向到特定的 GET 路由

app.get('/run-time',(req,res)=>{

})

so what i want is send the 'runtime' variable calulated in my POST req to my GET req here..how can i do this?所以我想要的是将在我的 POST req 中计算的“运行时”变量发送到我的 GET req 这里..我该怎么做?

I've never been use that way But I think you can use querystring querystring can contains data in url.我从来没有这样使用过,但我认为你可以使用查询字符串查询字符串可以包含 url 中的数据。

app.post('/run-time',(req,res)=>{
    const stoptime=req.body.stop
    const plannedtime=req.body.planned
    const runtime=plannedtime-stoptime 
    res.redirect(`/run-time?runtime={runtime}`);
})

app.get('/run-time',(req,res)=>{
    var runtime = req.query.runtime;
    //~ 
})

I think that way can be your solution.我认为这种方式可以成为您的解决方案。 But you have to change your code because It is not used like this.但是你必须改变你的代码,因为它不是这样使用的。 Maybe many solution is.也许很多解决方案是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM