嘿,大家刚刚开始使用nodejs和express,所以我想出了这样的情况,我想将我的数据从我的POST请求发送到我的GET请求。下面是下面的代码供您理解
app.post('/run-time',(req,res)=>{
const stoptime=req.body.stop
const plannedtime=req.body.planned
const runtime=plannedtime-stoptime
res.redirect('/run-time')
})
这是我的 POST 请求,我从表单中获取值,然后计算“运行时”,然后我必须重定向到特定的 GET 路由
app.get('/run-time',(req,res)=>{
})
所以我想要的是将我的 POST 请求中计算的“运行时”变量发送到我的 GET 请求。我该怎么做?
最佳答案
我从来没有这样使用过,但我认为你可以使用查询字符串 查询字符串可以包含 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;
//~
})
我认为这种方式可以是你的解决方案。 但是你必须更改你的代码,因为它不是这样使用的。 也许很多解决方案都是。
关于javascript - 在nodejs中从post请求发送数据到get请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58385226/