是否有任何方法可以通过express在express中向客户发送响应,并为单个页面渲染2次视图,还是有任何方法可以通过模板引擎将异步数据发送给客户端?
以下是我的代码,其中响应直接以HTML内容发送,我想用 pug 替换它。现在,问题是 wrest.write()将首先发生,并且 res.end()将在"诺言解决"后执行,我想在同样的执行顺序1接一个另一个事件都不是一次,因为两个事件都有时间差距。我有任何帮助吗?
console.log('File saved to', filename);
response.write('File downloaded to Server in DIR: '+filename+"<br>");
promiseCounter++;
if (promiseCounter == 15) {
afterDownloading();
}
}).catch((err) => {
afterDownloading();
throw err;
});
}
}
function afterDownloading()
{
var hrefString = "<br><a href='http://localhost:3000/displayKeywords'>Click here</a> to see all the keywords"+
" Entered yet!<br>";
response.end("Downloading Completed"+hrefString);
console.log("Downloading Completed!");
mongo.insert(fileDetail);
}
更好地实现目标的方法是通过Ajax更新UI。您可以为此遵循以下步骤:
- 渲染您的页面通常
- 在此页面的负载上触发AJAX请求。此AJAX请求将执行异步操作,并恢复更新UI所需的数据。
- 当您的AJAX请求得到响应时,更新UI。