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

拉特纳布·库马尔·赖

嘿伙计们刚刚开始使用 nodejs 并表达所以我想出了我想从我的 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')
})

这是我的 POST 请求,我从表单中获取值,然后计算“运行时”,然后我必须重定向到特定的 GET 路由

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

})

所以我想要的是将我的 POST req 中计算的“运行时”变量发送到我的 GET req 此处..我该怎么做?

振宇

我从来没有用过这种方式但我认为你可以使用 querystring querystring can contains data in 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;
    //~ 
})

我认为这种方式可以成为您的解决方案。但是你必须改变你的代码,因为它不是这样使用的。也许很多解决方案是。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章