【问题标题】:Sending data from post request to get request in nodejs从post请求发送数据以在nodejs中获取请求
【发布时间】:2019-10-14 23:26:30
【问题描述】:

嘿伙计们刚开始使用 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')
})

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

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

})

所以我想要的是将在我的 POST 请求中计算的“运行时”变量发送到我的 GET 请求这里..我该怎么做?

【问题讨论】:

标签: javascript node.js express


【解决方案1】:

我从来没有这样使用过,但我认为你可以使用查询字符串 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;
    //~ 
})

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

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多