javascript - 在nodejs中从post请求发送数据到get请求

标签 javascript node.js express

嘿,大家刚刚开始使用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/

相关文章:

javascript - promise 问题 : ReferenceError: reject is not defined

javascript - 如何观察数组或对象中任何属性的变化?

javascript - 我如何知道用户滑动的方向?

javascript - 在 if 语句中对数组使用 indexOf 的结果不一致

node.js - 从 Express 服务器响应中获取/读取 res.status() 值

javascript - 在express.js中,为什么当变量 'app'在其自己的定义中使用时没有错误?

javascript - Ionic 2 滚动性能问题

node.js - 使用 Node 和串行端口 Node 包在同一端口上发送/接收数据

node.js - 使用 Expressjs 的 Handlebars registerHelper 服务器端

javascript - 类实例的未定义​​返回 - Javascript