Jacob Wilson Jacob Wilson - 4 months ago 20
Node.js Question

Nodejs Expressjs app.get interval

I need to call app.get() multiple times to keep updating a page. I have a function which calls app.get every so often and I know the function writeToPage() gets called up app.get() only gets called once. How can keep updating the page with app.get() or is there a better/alternate way to achieve what I want. The reason I need this is that the content variable changes and I need to update the page.

function writeToPage(content, page) {
console.log('writeToPage Called');
app.get(`/${page}`, function (req, res) {
console.log('content Sent');


app.get() is for registering handlers that will called by Express when the client makes a matching http request. It is NOT something you call yourself repeatedly. You can't cause a browser page to auto-update in this manner. Besides, you only get to call res.send() once on the original page request. After it's been called once, the page has been sent to the browser and your work is done for that http request.

If you want a browser page to auto-update, you have these types of options:

  1. You can have the browser page auto-reload itself every so often. This can be done either with Javascript or with a meta refresh tag. This will reload a fresh copy of the page from your server and if the value has been updated, the new value will show.

  2. You can use a recurring ajax call in the web page to "poll" your server looking for a changed value and when that value is changed, then Javascript in your page can update that value in the current page to show the new value.

  3. You can use a webSocket or socket.io connection from the page to the server and then the server can automatically push a new value to the page whenever the value is updated. Javascript in the page would receive this new value and update the value in the current page to show the new value.