[英]Node.js Rerun page (and generate new data) upon page refresh
I have a web page running in Node.js (I an using ejs
to render pages using the default folder setup provided by WebStorm ). 我在Node.js的一个网页运行(我的使用
ejs
来渲染使用WebStorm提供的默认文件夹设置页)。 Currently I run node bin/www
which contains the following: 当前,我运行
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***
This then runs the app.js
file which contains the following: 然后,它将运行包含以下内容的
app.js
文件:
...
var predictions = require("./routes/predictions");
app.use("/predictions", predictions);
...
This is where the issue lies . 这就是问题所在 。 In the javascript file for this page (
routes/predictions.js
) I want to display information collected from two remote sources ( www.source_json.com
and www.source_csv.com
).(As there are only two sources I have decided to do the web requests synchronously). 在此页面的javascript文件中(
routes/predictions.js
),我想显示从两个远程源( www.source_json.com
和www.source_csv.com
)收集的信息。(由于我决定只做两个源网络同步请求)。 This is private information which I don't want the end user to be able to access, so I thought it would be best to process and merge these data server side and then any final data I want the user to be able to see is saved into a javascript object called data_array
and is sent to the corresponding ejs
page ( views/predictions.ejs
) via: 这是我不希望最终用户能够访问的私人信息,因此我认为最好处理并合并这些数据服务器端,然后保存我希望用户能够看到的所有最终数据。放入名为
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;
The data collected from the two remote sources changes about every 20 mins. 从两个远程源收集的数据大约每20分钟更改一次。 However, when I refresh the page the data isn't recollected from the two data sources (I have verified this using some timestamps).
但是,当我刷新页面时,不会从两个数据源中收集数据(我已经使用一些时间戳对此进行了验证)。 How can I ask Node.js to return freshly sourced data each time the page is requested?
每次请求页面时,我如何要求Node.js返回新鲜数据? Is there an
update button
I can create on the page, or some javascript I can add? 我可以在页面上创建一个
update button
,还是可以添加一些JavaScript? Currently the only way I can refresh the info is to restart the server ( CTRL + C
+ node bin/www
). 当前,刷新信息的唯一方法是重新启动服务器(
CTRL + C
+ node bin/www
)。
So far I have tried using setInterval
loops, while
loops, adding <% setTimeout('window.location.reload();', 20000); %>
到目前为止,我已经尝试过使用
setInterval
循环和while
循环,添加<% setTimeout('window.location.reload();', 20000); %>
<% setTimeout('window.location.reload();', 20000); %>
. <% setTimeout('window.location.reload();', 20000); %>
。 Additionally I have considered using nodemon
to watch some pseudo file which could contain a time when the data sources last changed the information and then restart the server if it does change, but I would like to avoid this. 另外,我已经考虑过使用
nodemon
来监视一些伪文件,该伪文件可能包含数据源上次更改信息的时间,然后在确实发生更改的情况下重新启动服务器,但是我想避免这种情况。
Additionally I have looked at "similar" topics: 另外,我研究了“类似”主题:
Check if this works: 检查是否可行:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.