简体   繁体   English

如何从nodejs应用程序访问浏览器的窗口对象?

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

I'm trying to write a simple app to watch over files changes and automatically reload the updated code in the browser. 我正在尝试编写一个简单的应用程序来监视文件更改并自动在浏览器中重新加载更新的代码。 I'm aware of the existance of livereload nodeamon and others, i just wanted to write my own. 我知道livereload nodeamon和其他人的存在,我只想写自己的。 I've created the server, let it read the file i want to read, called the watcher that kills and restart the server when changes happen in the watched file. 我创建了服务器,让它读取我想要读取的文件,称为观察者,当观察文件发生变化时,它会杀死并重新启动服务器。 Last part: it should refresh the browser. 最后一部分:它应该刷新浏览器。 how is this possible? 这怎么可能?

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. 正如其他人所解释的那样,浏览器编程环境以及window.location.reload()与node.js完全分开,因此您无法直接从node.js调用它。 Server-side code runs on the server in node.js. 服务器端代码在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. 这两者可以通过Ajax请求或通过webSocket连接发送的消息进行通信,但是它们不能直接相互调用代码。


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). 浏览器中的Javascript可以定期询问服务器是否有任何新内容(通常使用传递时间戳的Ajax调用)。 If the server says there is something new since that timestamp, then the Javascript in the browser can request the new data/file. 如果服务器说自那个时间戳以来有新的东西,那么浏览器中的Javascript可以请求新的数据/文件。 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. 浏览器可以与服务器建立持久的webSocket连接。 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. 然后,接收此消息的所有连接客户端都可以进行正常的Ajax调用以检索新数据。 Or, the new info can just be directly sent to the client over the webSocket. 或者,新信息可以通过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. 使用webSocket选项,您不希望服务器重新启动,因为这会丢弃所有webSocket连接。 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. 我不确定为什么你当前正在重新启动服务器,但是你必须改变它以使用webSocket解决方案。

NodeJS does not have a window variable that represents the global namespace. NodeJS没有表示全局命名空间的window变量。 Instead it has a variable called global . 相反,它有一个名为global的变量。

There is no browser, window or URL location in NodeJS. NodeJS中没有浏览器,窗口或URL位置。

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. 在node.js中,您有一个process ,它是一个node.js全局对象,如window ,它是一个浏览器全局对象。

For more info type process into the node.js shell. 欲了解更多信息类型process成Node.js的外壳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问浏览器 Window 对象中的 JavaScript 类及其方法? - How can I access JavaScript Class and it's methods in browser Window object? 如何从 ActiveXObject 访问 window.document object? - How can I access window.document object from ActiveXObject? 如何在浏览器中访问全局对象? - how can I access global object in browser? 如何在同一浏览器窗口中从另一表单中的一个表单访问变量? - How can I access a variable from one Form in a different Form inside the same browser window? 如何访问新打开的选项卡的窗口对象? [在Firefox扩展程序中] - How can I access a newly-opened tab's window object? [in a firefox extension] 从对象数组的元素中,如何访问对象的属性/方法。 JS - From an Object's Array's Element, how can I access the Object's properties/methods. Js 我如何通过管道将mysql数据传递到浏览器窗口而不是Nodejs中的控制台? - How can i pipe mysql data to a browser window instead of the console in Nodejs? 我如何从 ReactJS 访问当前 NodeJS 中的环境变量 - How can i access evironment variables in present NodeJS from ReactJS 如何从指纹扫描仪获取数据到NodeJS应用程序? - How can I get data from a fingerprint scanner in to a NodeJS application? 如何在选项卡的主窗口对象上定义属性? - How can I define a property on the tab's main window object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM