gpt4 book ai didi

javascript - Express.js res.render() 和 res.redirect()

转载 作者:搜寻专家 更新时间:2023-11-01 00:40:38 25 4
gpt4 key购买 nike

我的 express 应用程序中有一条路线,它应该执行以下操作:

  • 从外部获取一些数据(OK)
  • 显示带有 socket.io 监听消息的 HTML 页面(确定)
  • 执行一些计算,这需要很长时间
  • 在每个完成后通过 socket.io 发送消息(OK)
  • 当所有计算完成后,显示结果页面(问题)

所以,我的代码的简化版本是:

module.exports = function(io) { // This is so I can use socket.io inside the route
var express = require('express');
var router = express.Router();

[... and other required]

router.post('/', function(req, res, next) {
res.render('loading'); // This renders the template which holds accepts and renders socket.io messages
pefromCalculaton();
sendSocketIOMessage();
session.data = resultData; // I get the result of all calculations and put that in the session. It's a quick fix, don't judge, I've got no presistancy in my project as for now...
res.redirect('results'); // And here I'd like to go to the other route, which will display the data, getting it from the session.
});
return router;
}

因为这行不通,我可能想在这里做一些非常愚蠢的事情。但我真正想做的是:

  • 执行计算
  • 在执行计算时,使用套接字更新进度
  • 计算完成后,渲染一个模板,显示所有结果。

最佳答案

好吧,我的 friend ,如您所知,一个请求中不能发送两个响应。所以这就是您需要做的。

module.exports = function(io) { // This is so I can use socket.io inside the route
var express = require('express');
var router = express.Router();

[... and other required]

router.post('/', function(req, res, next) {
var taskId = (new Date()).getTime(); //timestamp to get unique task id.
res.render('loading');
startCalculations(function(progressIndicator){ //progress callback
io.emit('progress',progressIndicator);
},function(result){ // Finish callback
session[taskId] = result;
io.emit('finish',{ taskid: taskId });
});
});


router.get('/result:taskId', function(req, res, next) {
var result = session[req.params.taskId];
if(!result)
{
//Result already consumed!
res.render('expired');
return;
}
delete session[req.params.taskId];
res.render('result', {result: result});
});

//progress callback will be called when we want to report progress
//done callback indicates our results are ready.
function startCalculations(progress, done){
//This is just a stupid piece of code, just to emulate loading.
//Your awesome async code will replace this
//In your case it will not be a loop and there will be a callback
//To signal finish.
var result = 0;
for(i = 0; i < 100; i++)
{
result = result + i;
//Simulate progress
progress(i);
}
//Simulate finish -- result has 0 to 99 all added up ;)
done(result);

}


return router;
}

现在在 html 前端你可以有 ...

this is how your loading view would look like.

<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();

//Init code.

socket.on('progress', function(progressIndicator){
//Your jquery mojo to show progress
displayProgress(progressIndicator);
});

socket.on('finish', function(task){
//Redirect to result page from frontend (you were trying to do
//on backend -- node.js)
window.location.href = "/result/" + task.taskId;
//OR
//window.location.replace("/result/" + task.taskId);
});
</script>

希望这是有道理的并有助于...

如果您还需要什么,请告诉我。

玩得开心!

关于javascript - Express.js res.render() 和 res.redirect(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35720587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com