从发布请求发送数据以在Node.js中获取请求

时间:2019-10-14 23:26:30

标签: javascript node.js express

伙计们刚开始使用nodejs进行表达,所以我想到了一种情况,我想将数据从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请求。我该怎么办?

1 个答案:

答案 0 :(得分:1)

我从未使用过这种方式,但是我认为您可以使用querystring querystring可以包含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;
    //~ 
})

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

相关问题