从发布请求发送数据以在nodejs中获取请求
[伙计们刚开始使用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请求..我该怎么做?
回答如下:我从未使用过这种方式,但是我认为您可以使用querystringquerystring可以包含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;
//~
})
我认为这样可以成为您的解决方案。但是您必须更改代码,因为它不是这样使用的。也许有很多解决方案。
从发布请求发送数据以在nodejs中获取请求
[伙计们刚开始使用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请求..我该怎么做?
回答如下:我从未使用过这种方式,但是我认为您可以使用querystringquerystring可以包含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;
//~
})
我认为这样可以成为您的解决方案。但是您必须更改代码,因为它不是这样使用的。也许有很多解决方案。