执行工作后,我如何在Node.js中自动重新加载页面

时间:2019-01-20 07:20:53

标签: node.js promise ejs

我正在主页上获取json数据。在加载json数据之前,页面应显示加载文本。完全加载json数据后,应完全显示json数据。

var express = require('express');
'use strict';
var https = require('https');
var app =  express();
var request = require('request');
var bodyParser =require('body-parser');
var task = [];
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(3000, function(){
console.log('Example app listening on port 3000 !');
});
function initialize() {
var options = {
url: 'https://jsonplaceholder.typicode.com/posts',
headers: {'User-Agent': 'request'}
};
// Return new promise 
return new Promise(function(resolve, reject) {
// Do async job
request.get(options, function(err, resp, body) {
if (err) {
reject(err);
} else {
resolve(JSON.parse(body));
}
})
})
}
app.get('/', function(req, res){
if(task.length === 0){
var initializePromise = initialize();
initializePromise.then(function(result) {
task = result;
// res.redirect('/test');
console.log("task initialized");
if(counter ===0){
res.render('index', {task: task.splice(0, lNum), loading: false});
}
}, function(err) { console.log(err); });       
}else{
console.log("task already initialized");
}
if(task.length ===0){
res.render('index', {task: null, loading: true});
}else{
res.render('index', {task: task, loading: false });
}  
});

当我运行脚本时。它给了我以下错误

Example app listening on port 3000 !
task initialized
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at validateHeader (_http_outgoing.js:503:11)
    at ServerResponse.setHeader (_http_outgoing.js:510:3)
    at ServerResponse.header (C:\Users\Crustalmaa\Documents\sassproject\reactapp\job\node_modules\express\lib\response.js:767:10)
    at ServerResponse.send (C:\Users\Crustalmaa\Documents\sassproject\reactapp\job\node_modules\express\lib\response.js:170:12)
    at done (C:\Users\Crustalmaa\Documents\sassproject\reactapp\job\node_modules\express\lib\response.js:1004:10)
    at tryHandleCache (C:\Users\Crustalmaa\Documents\sassproject\reactapp\job\node_modules\ejs\lib\ejs.js:257:5)
    at View.exports.renderFile [as engine] (C:\Users\Crustalmaa\Documents\sassproject\reactapp\job\node_modules\ejs\lib\ejs.js:482:10)
    at View.render (C:\Users\Crustalmaa\Documents\sassproject\reactapp\job\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\Crustalmaa\Documents\sassproject\reactapp\job\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\Crustalmaa\Documents\sassproject\reactapp\job\node_modules\express\lib\application.js:592:3)
Task already initialized.

当我在app.get('/',function(){})中评论res.render方法之一时 而且我不知道如何重定向或更新index.ejs。在index.ejs

<% if(loading){%>
<p>Loading</p>
<%} else{%>
<% for( var i = 0; i < task.length; i++){ %>
<li><input type="checkbox" name="check" value="<%= task[i] %>" /> <%= task[i].id %> </li>
<% } } %>
<a href  = "/test">Load more</a>

当我运行localhost:3000时 first screenshot 2秒后刷新浏览器,它给了我以下结果 second screenshot 而且我不想手动刷新浏览器。内容加载完成后,它将自动刷新或更新内容。我该怎么做并修复错误消息?

0 个答案:

没有答案