【发布时间】:2016-06-13 17:36:42
【问题描述】:
我的快递应用中有一条路线,它应该执行以下操作:
- 从外部获取一些数据(OK)
- 显示带有 socket.io 监听消息的 HTML 页面(OK)
- 执行一些需要很长时间的计算
- 每一个完成后通过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;
}
由于这不起作用,我可能正在尝试在这里做一些非常愚蠢的事情。但我真正想做的是:
- 执行计算
- 在执行计算时,使用套接字更新进度
- 计算完成后,渲染一个模板,显示所有结果。
【问题讨论】:
标签: javascript node.js express routes