How can I access the browser's window object from a nodejs application? How can I access the browser's window object from a nodejs application? node.js node.js

How can I access the browser's window object from a nodejs application?


As others have explained, the browser programming environment and thus window.location.reload() is completely separate from node.js so you cannot call that directly from node.js. Server-side code runs on the server in node.js. Client-side code in the browser runs in the browser only. The two may communicate via Ajax requests or messages sent over a webSocket connection, but they can't call code in each other directly.


For a browser to refresh based on something that changes on the server, there are two basic approaches.

  1. Javascript in the browser can regularly ask the server if it has anything new (usually with an Ajax call that passes a timestamp). If the server says there is something new since that timestamp, then the Javascript in the browser can request the new data/file. This "polling" approach is not particularly efficient because if the data doesn't change very often, then most of the requests for something new will just return that there is nothing new. It's also not friendly for battery life.

  2. The browser can make a lasting webSocket connection to the server. Then, when the server finds that something has changed on the server, it can just directly send a notification to each connected browser informing it that there is something new. All connected clients that receive this message can then make a normal Ajax call to retrieve the new data. Or, the new info can just be directly sent to the client over the webSocket. In either case, this direct notification is generally more efficient than the "polling" solution.

With the webSocket option, you will NOT want your server to restart because that will drop all webSocket connections. I'm not sure why you're currently restarting the server when something changes, but you will have to change that to use the webSocket solution.


NodeJS does not have a window variable that represents the global namespace. Instead it has a variable called global.

There is no browser, window or URL location in NodeJS.

It's a purely server side tool.


In node.js you have process which is a node.js global object like window which is a browser global object.

For more info type process into the node.js shell.