Node.js 在页面刷新时重新运行页面(并生成新数据)
Node.js Rerun page (and generate new data) upon page refresh
我在 Node.js 中有一个网页 运行ning(我正在使用 ejs
使用 WebStorm[=51 提供的默认文件夹设置呈现页面=]).目前我 运行 node bin/www
其中包含以下内容:
***preamble***
var app = require('../app');
var debug = require('debug')('tennis-geek:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
***rest of code***
这就是 运行 包含以下内容的 app.js
文件:
...
var predictions = require("./routes/predictions");
app.use("/predictions", predictions);
...
问题就出在这里。在此页面的 javascript 文件中 (routes/predictions.js
) 我想显示从两个远程源(www.source_json.com
和 www.source_csv.com
)收集的信息。(因为我只有两个源已决定同步执行 Web 请求)。这是我不希望最终用户能够访问的私人信息,所以我认为最好在服务器端处理和合并这些数据,然后保存我希望用户能够看到的任何最终数据进入名为 data_array
的 javascript 对象,并通过以下方式发送到相应的 ejs
页面 (views/predictions.ejs
):
var express = require('express');
var router = express.Router();
var recline = ("reclinejs");
var papa = require("papaparse");
var request = require('sync-request');
//Now I collect the Data.
var res = request('GET', 'www.source_json.com');
var data_1_json = JSON.parse(res.getBody('utf8'));
res = request('GET', 'www.source_csv.com');
var data_2_json = papa.parse(res.getBody('utf8'), {header: true, skipEmptyLines: true}).data;
//Now I do some private number crunching on the data
var data_array = private_number_crunching(data_1_json, data_2_json);
//Now I send make this information available to the user's browser
router.get('/', function (req, res, next) {
res.render('predictions', {title: 'Predictions', data_from_js: data_array});
});
module.exports = router;
从两个远程源收集的数据大约每 20 分钟更改一次。但是,当我刷新页面时,不会从两个数据源重新收集数据(我已经使用一些时间戳验证了这一点)。 如何在每次请求页面时向 Node.js 请求 return 新鲜来源的数据? 是否有 update button
我可以在页面上创建,或者一些javascript我可以添加吗?目前我可以刷新信息的唯一方法是重新启动服务器 (CTRL + C
+ node bin/www
).
到目前为止,我已经尝试使用 setInterval
循环,while
循环,添加 <% setTimeout('window.location.reload();', 20000); %>
。此外,我考虑过使用 nodemon
来观看一些伪文件,其中可能包含数据源上次更改信息的时间,然后在它发生更改时重新启动服务器,但我想避免这种情况。
此外,我还查看了 "similar" 个主题:
- How do I force a page to refresh the data upon reload after entering new record
- node.js get new data without refresh
检查这是否有效:
var express = require('express');
var router = express.Router();
var recline = ("reclinejs");
var papa = require("papaparse");
var request = require('sync-request');
//Now I collect the Data.
//Now I send make this information available to the user's browser
router.get('/', function (req, res, next) {
var response1 = request('GET', 'www.source_json.com');
var data_1_json = JSON.parse(response1.getBody('utf8'));
var response2 = request('GET', 'www.source_csv.com');
var data_2_json = papa.parse(response2.getBody('utf8'), {header: true, skipEmptyLines: true}).data;
//Now I do some private number crunching on the data
var data_array = private_number_crunching(data_1_json, data_2_json);
res.render('predictions', {title: 'Predictions', data_from_js: data_array});
});
module.exports = router;
我在 Node.js 中有一个网页 运行ning(我正在使用 ejs
使用 WebStorm[=51 提供的默认文件夹设置呈现页面=]).目前我 运行 node bin/www
其中包含以下内容:
***preamble***
var app = require('../app');
var debug = require('debug')('tennis-geek:server');
var http = require('http');
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
***rest of code***
这就是 运行 包含以下内容的 app.js
文件:
...
var predictions = require("./routes/predictions");
app.use("/predictions", predictions);
...
问题就出在这里。在此页面的 javascript 文件中 (routes/predictions.js
) 我想显示从两个远程源(www.source_json.com
和 www.source_csv.com
)收集的信息。(因为我只有两个源已决定同步执行 Web 请求)。这是我不希望最终用户能够访问的私人信息,所以我认为最好在服务器端处理和合并这些数据,然后保存我希望用户能够看到的任何最终数据进入名为 data_array
的 javascript 对象,并通过以下方式发送到相应的 ejs
页面 (views/predictions.ejs
):
var express = require('express');
var router = express.Router();
var recline = ("reclinejs");
var papa = require("papaparse");
var request = require('sync-request');
//Now I collect the Data.
var res = request('GET', 'www.source_json.com');
var data_1_json = JSON.parse(res.getBody('utf8'));
res = request('GET', 'www.source_csv.com');
var data_2_json = papa.parse(res.getBody('utf8'), {header: true, skipEmptyLines: true}).data;
//Now I do some private number crunching on the data
var data_array = private_number_crunching(data_1_json, data_2_json);
//Now I send make this information available to the user's browser
router.get('/', function (req, res, next) {
res.render('predictions', {title: 'Predictions', data_from_js: data_array});
});
module.exports = router;
从两个远程源收集的数据大约每 20 分钟更改一次。但是,当我刷新页面时,不会从两个数据源重新收集数据(我已经使用一些时间戳验证了这一点)。 如何在每次请求页面时向 Node.js 请求 return 新鲜来源的数据? 是否有 update button
我可以在页面上创建,或者一些javascript我可以添加吗?目前我可以刷新信息的唯一方法是重新启动服务器 (CTRL + C
+ node bin/www
).
到目前为止,我已经尝试使用 setInterval
循环,while
循环,添加 <% setTimeout('window.location.reload();', 20000); %>
。此外,我考虑过使用 nodemon
来观看一些伪文件,其中可能包含数据源上次更改信息的时间,然后在它发生更改时重新启动服务器,但我想避免这种情况。
此外,我还查看了 "similar" 个主题:
- How do I force a page to refresh the data upon reload after entering new record
- node.js get new data without refresh
检查这是否有效:
var express = require('express');
var router = express.Router();
var recline = ("reclinejs");
var papa = require("papaparse");
var request = require('sync-request');
//Now I collect the Data.
//Now I send make this information available to the user's browser
router.get('/', function (req, res, next) {
var response1 = request('GET', 'www.source_json.com');
var data_1_json = JSON.parse(response1.getBody('utf8'));
var response2 = request('GET', 'www.source_csv.com');
var data_2_json = papa.parse(response2.getBody('utf8'), {header: true, skipEmptyLines: true}).data;
//Now I do some private number crunching on the data
var data_array = private_number_crunching(data_1_json, data_2_json);
res.render('predictions', {title: 'Predictions', data_from_js: data_array});
});
module.exports = router;