为什么没有节点。js有自己的DOM吗?

时间:2022-09-29 07:18:52

When I discovered NodeJS was built on the V8 Javascript engine I thought "Great, web scraping will be easier, as the page will be rendered like in the browser, with a 'native' DOM there supporting XPath, and any AJAX calls in the page executed."

当我在V8 Javascript引擎上发现NodeJS时,我认为“很好,web抓取会更容易,因为页面会在浏览器中呈现,有一个‘本地’DOM支持XPath,并且在页面中执行任何AJAX调用。”

  1. Why when it uses the same JS engine as Chrome doesn't it have a native DOM?
  2. 为什么当它使用与Chrome相同的JS引擎时,它没有本地DOM?
  3. Likewise, why doesn't it have a mode to run JS in retrieved pages?
  4. 同样地,为什么在检索的页面中它没有运行JS的模式呢?
  5. What am I not understanding about Javascript engines vs the engine in a web browser? :)
  6. 我对Javascript引擎和web浏览器引擎的理解是什么?:)

Many thanks!

很多谢谢!

12 个解决方案

#1


45  

The DOM is the DOM, and the JavaScript implementation is simply a separate entity. The DOM represents a set of facilities that a web browser exposes to the JavaScript environment. There's no requirement however that any particular JavaScript runtime will have any facilities exposed via the global object.

DOM是DOM,而JavaScript实现只是一个单独的实体。DOM表示web浏览器向JavaScript环境公开的一组工具。但是,没有任何要求,任何特定的JavaScript运行时都会有任何通过全局对象公开的工具。

What Node.js is is a stand-alone JavaScript environment completely independent of a web browser. There's no intrinsic link between web browsers and JavaScript; the DOM is not part of the JavaScript language or specification or anything.

什么节点。js是一个完全独立于web浏览器的独立JavaScript环境。web浏览器和JavaScript之间没有内在联系;DOM不是JavaScript语言或规范的一部分。

I use the old Rhino Java-based JavaScript implementation in my Java-based web server. That environment also has nothing at all to do with any DOM. It's my own application that's responsible for populating the global object with facilities to do what I need it to be able to do, and it's not a DOM.

我在基于java的web服务器中使用了老的基于java的JavaScript实现。这种环境与任何DOM都没有任何关系。这是我自己的应用程序,它负责填充全局对象,让它能做我需要它做的事情,它不是DOM。

Note that there are projects like jsdom if you want a virtual DOM in your Node project. Because of its very nature as a server-side platform, a DOM is a facility that Node can do without and still make perfect sense for a wide variety of server applications. That's not to say that a DOM might not be useful to some people, but it's just not in the same category of services as things like process control, I/O, networking, database interop, and so on.

请注意,如果您希望在节点项目中使用虚拟DOM,则有类似jsdom的项目。由于其本质上是一个服务器端平台,DOM是一个节点可以不使用的工具,而且对于各种各样的服务器应用程序来说,它仍然是很有意义的。这并不是说DOM对某些人来说可能不是有用的,但它只是与过程控制、I/O、网络、数据库互操作等类似的服务类别不同。

There may be some "official" answer to the question "why?" out there, but it's basically just the business of those who maintain Node (the Node Foundation now). If some intrepid developer out there decides that Node should ship by default with a set of modules to support a virtual DOM, and successfully works and works and makes that happen, then Node will have a DOM.

对于“为什么”这个问题,可能有一些“官方”的答案,但它基本上只是那些维护节点(现在是节点基础)的业务。如果有一些勇敢的开发人员决定,节点应该默认使用一组模块来支持虚拟DOM,并且成功地工作和工作并使之发生,那么Node将拥有一个DOM。

#2


15  

P.S: When reading this question I was also wondering if V8(node.js is built in top of this) had a DOM

P。S:在阅读这个问题时,我还想知道V8(node)。js建立在这个之上)有一个DOM。

Why when it uses the same JS engine as Chrome doesn't it have a native DOM?

为什么当它使用与Chrome相同的JS引擎时,它没有本地DOM?

But I searched google and found Google's V8 page which recites the following:

但是我搜索了谷歌,发现了谷歌的V8页面,它的内容如下:

JavaScript is most commonly used for client-side scripting in a browser, being used to manipulate Document Object Model (DOM) objects for example. The DOM is not, however, typically provided by the JavaScript engine but instead by a browser. The same is true of V8—Google Chrome provides the DOM. V8 does however provide all the data types, operators, objects and functions specified in the ECMA standard.

JavaScript通常用于浏览器中的客户端脚本,用于操作文档对象模型(DOM)对象。但是,DOM通常不是由JavaScript引擎提供的,而是由浏览器提供的。V8-Google Chrome也提供了DOM。但是,V8提供了ECMA标准中指定的所有数据类型、操作符、对象和函数。

node.js uses V8 and not Google Chrome.

节点。js使用的是V8而不是谷歌。

Likewise, why doesn't it have a mode to run JS in retrieved pages?

同样地,为什么在检索的页面中它没有运行JS的模式呢?

I also think we don't really need it that bad. Ryan Dahl created node.js as one man(single programmer). Maybe now he(his team) will develop this, but I was already extremely amazed by the amount of code he produced(crazy). He wanted to make non-blocking easy/efficient library , which I think he did a mighty good job at.

我也认为我们不需要那么糟糕。Ryan Dahl创建节点。作为一个人(单个程序员)。也许现在他(他的团队)会开发这个,但是我已经对他的代码(疯狂)的数量感到非常惊讶。他想让非阻塞的简易/高效的图书馆,我认为他在这方面做得很好。

But then again another developer created a module which is pretty good and actively developed(today) at https://github.com/tmpvar/jsdom.

但是,另一个开发人员创建了一个非常好的模块,并在https://github.com/tmpvar/jsdom中得到了很好的开发(今天)。

What am I not understanding about Javascript engines vs the engine in a web browser? :)

我对Javascript引擎和web浏览器引擎的理解是什么?:)

Those are different things as is hopefully clear from the quote above.

这些是不同的东西,希望从上面的引用中可以清楚地看到。

#3


6  

node.js chose not to include it in their standard library. For any functionality, there is an inevitable tradeoff between comprehensiveness, scalability, and maintainability.

节点。js选择不把它包含在他们的标准库中。对于任何功能,在全面性、可伸缩性和可维护性之间存在不可避免的折衷。

That doesn't mean it's not potentially useful. There is at least one JavaScript DOM implementation intended for NodeJS (among other CommonJS implementations).

这并不意味着它没有潜在的用处。至少有一个JavaScript DOM实现是针对node . js的(在其他的CommonJS实现中)。

#4


4  

You seem to have a flawed assumption that V8 and the DOM are inextricably related, that's not the case. The DOM is actually handled by Webkit, V8 doesn't handle the DOM, it handles Javascript calls to the DOM. Don't let this discourage you, Node.js has carved out a significant niche in the realtime server market, but don't let anybody tell you it's just for servers. Node makes it possible to build almost anything with JavaScript.

您似乎有一个错误的假设,即V8和DOM是不可分割的相关的,事实并非如此。DOM实际上是由Webkit处理的,V8不处理DOM,它处理对DOM的Javascript调用。不要因此气馁,Node。js在realtime服务器市场上开辟了一个重要的利基市场,但不要让任何人告诉你它只是针对服务器的。Node使用JavaScript构建几乎任何东西成为可能。

It is possible to do what you're talking about. For example there is the very good jsdom library if you really need access to the DOM, and node-htmlparser, there are also some really good scraping libraries that take advantage of these like apricot.

你所谈论的事情是有可能实现的。例如,如果您确实需要访问DOM和node-htmlparser,那么就有非常好的jsdom库,还有一些非常好的抓取库,它们利用了这些类似于apricot的资源。

#5


4  

This is related: There is a new project (2012) called node-webkit which tries to add DOM and a lot more from Webkit to Node. Support it!

这是相关的:有一个新的项目(2012)叫做Node - Webkit,它试图添加DOM和更多的Webkit到Node。支持它!

#6


2  

To answer your underlying question, you can use JSDom and jQuery to scrape pages in node.js: http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs

为了回答您的基本问题,您可以使用JSDom和jQuery在节点中抓取页面。js:http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs

I've used this approach a few times myself, and it works great.

我自己用过几次这种方法,效果很好。

#7


0  

Javascript != browser. Javascript as a language is not tied to browsers; node.js is simply an implementation of Javascript that is intended for servers, not browsers. Hence no DOM.

浏览器的Javascript ! =。Javascript作为一种语言并不与浏览器绑定;节点。js只是一个Javascript的实现,它是针对服务器而不是浏览器的。因此没有DOM。

#8


0  

If you read DOM as 'linked objects immediately accessible from my script' then the answer 'it does, but it's very different from set of objects available from web document script'. The main reason is that node is 'evented I/O for V8', not 'HTML tree objects for V8'

如果您将DOM作为“链接对象”,可以立即从我的脚本中访问,那么答案是“它可以,但是它与从web文档脚本中获得的对象集非常不同”。最主要的原因是,node是“V8的I/O”,而不是“V8的HTML树对象”。

#9


0  

It seems people have answered 'why' but not how. A quick answer of how is that in a web browser, a document object is exposed (hence DOM , document object model). On windows this object is called document object. You can refer to this page and look at the methods it exposes which are for handling HTML documents like createElement. I don't use node.js or haven't done COM programming in a while but I'd imagine you could use DOM in node.js by simply calling the COM object IHTMLDocument3. Of course for other platforms like Mac OS X or Linux you would probably have to use something from their OS api. This should allow you to easily build a webpage server side using DOM, or to scrape incoming web pages.

似乎人们已经回答了“为什么”,而不是如何回答。一个快速的答案是,在web浏览器中,文档对象被公开(因此DOM,文档对象模型)。在windows上,这个对象称为文档对象。您可以参考该页面并查看它公开的方法,这些方法用于处理像createElement这样的HTML文档。我不使用节点。js或者还没有做过COM编程,但是我想你可以在node中使用DOM。通过简单地调用COM对象IHTMLDocument3。当然,对于其他平台,如Mac OS X或Linux,你可能不得不使用它们的OS api。这应该允许您使用DOM轻松构建一个网页服务器端,或者抓取传入的web页面。

#10


-1  

Node.js is for serverside programming. There is no DOM to be rendered in the server.

节点。js用于服务器端编程。在服务器中没有呈现DOM。

#11


-1  

2018 answer: mainly for historical reasons, but this may change in future.

2018年答案:主要是出于历史原因,但这可能会在未来发生变化。

Historically, very little DOM manipulation was done on the server. Addiotinally, as other answers allude, the JS stdlib and the DOM are seperate libraries - if you're using node, for, say, Unix scripting, then HTMLElement and NodeList etc aren't really relevant to that.

历史上,在服务器上做的DOM操作很少。另外,作为其他的答案,JS stdlib和DOM是分离的库——如果您使用的是node,比如Unix脚本,那么HTMLElement和NodeList等与此并不相关。

However: server-side DOM manipulation is now a very common part of delivering web apps. Web servers need to understand the structure of pages, and, if asked to render a resource as HTML, deliver HTML content that reflects the initial state of a web application. This means web apps load much faster than if the server simply delivers a stub page and has the browsers then do the work of filling in the real content. Currently this is done with JSDom and similar, but in the same way node has Request and Response objects built in, having DOM functions maintained as part of the stdlib would help with this task.

不过,服务器端DOM操作现在是交付web应用程序的一个非常常见的部分。Web服务器需要了解页面的结构,如果要求将资源呈现为HTML,则提供反映Web应用程序初始状态的HTML内容。这意味着web应用程序的加载速度要快于服务器只提供一个存根页面,并让浏览器完成填充实际内容的工作。目前,这是用JSDom和类似的方法完成的,但是在相同的方式下,node拥有构建的请求和响应对象,在stdlib中维护的DOM函数将有助于完成这个任务。

#12


-4  

1) What does it mean for it to have a D ocument O bject M odel? There's no document to represent.

1)有一个D的薪水是什么意思?没有文件可以表示。

2) You're most of the time you're not retrieving pages. You can, but most Node apps probably won't be.

2)你大部分时间都没有检索页面。你可以,但大多数节点应用程序可能不会。

3) Without a document and a browser, Javascript is just another programming language. So you may ask why there isn't a DOM in C# or Java

没有文档和浏览器,Javascript只是另一种编程语言。因此,您可能会问为什么c#或Java中没有DOM。

#1


45  

The DOM is the DOM, and the JavaScript implementation is simply a separate entity. The DOM represents a set of facilities that a web browser exposes to the JavaScript environment. There's no requirement however that any particular JavaScript runtime will have any facilities exposed via the global object.

DOM是DOM,而JavaScript实现只是一个单独的实体。DOM表示web浏览器向JavaScript环境公开的一组工具。但是,没有任何要求,任何特定的JavaScript运行时都会有任何通过全局对象公开的工具。

What Node.js is is a stand-alone JavaScript environment completely independent of a web browser. There's no intrinsic link between web browsers and JavaScript; the DOM is not part of the JavaScript language or specification or anything.

什么节点。js是一个完全独立于web浏览器的独立JavaScript环境。web浏览器和JavaScript之间没有内在联系;DOM不是JavaScript语言或规范的一部分。

I use the old Rhino Java-based JavaScript implementation in my Java-based web server. That environment also has nothing at all to do with any DOM. It's my own application that's responsible for populating the global object with facilities to do what I need it to be able to do, and it's not a DOM.

我在基于java的web服务器中使用了老的基于java的JavaScript实现。这种环境与任何DOM都没有任何关系。这是我自己的应用程序,它负责填充全局对象,让它能做我需要它做的事情,它不是DOM。

Note that there are projects like jsdom if you want a virtual DOM in your Node project. Because of its very nature as a server-side platform, a DOM is a facility that Node can do without and still make perfect sense for a wide variety of server applications. That's not to say that a DOM might not be useful to some people, but it's just not in the same category of services as things like process control, I/O, networking, database interop, and so on.

请注意,如果您希望在节点项目中使用虚拟DOM,则有类似jsdom的项目。由于其本质上是一个服务器端平台,DOM是一个节点可以不使用的工具,而且对于各种各样的服务器应用程序来说,它仍然是很有意义的。这并不是说DOM对某些人来说可能不是有用的,但它只是与过程控制、I/O、网络、数据库互操作等类似的服务类别不同。

There may be some "official" answer to the question "why?" out there, but it's basically just the business of those who maintain Node (the Node Foundation now). If some intrepid developer out there decides that Node should ship by default with a set of modules to support a virtual DOM, and successfully works and works and makes that happen, then Node will have a DOM.

对于“为什么”这个问题,可能有一些“官方”的答案,但它基本上只是那些维护节点(现在是节点基础)的业务。如果有一些勇敢的开发人员决定,节点应该默认使用一组模块来支持虚拟DOM,并且成功地工作和工作并使之发生,那么Node将拥有一个DOM。

#2


15  

P.S: When reading this question I was also wondering if V8(node.js is built in top of this) had a DOM

P。S:在阅读这个问题时,我还想知道V8(node)。js建立在这个之上)有一个DOM。

Why when it uses the same JS engine as Chrome doesn't it have a native DOM?

为什么当它使用与Chrome相同的JS引擎时,它没有本地DOM?

But I searched google and found Google's V8 page which recites the following:

但是我搜索了谷歌,发现了谷歌的V8页面,它的内容如下:

JavaScript is most commonly used for client-side scripting in a browser, being used to manipulate Document Object Model (DOM) objects for example. The DOM is not, however, typically provided by the JavaScript engine but instead by a browser. The same is true of V8—Google Chrome provides the DOM. V8 does however provide all the data types, operators, objects and functions specified in the ECMA standard.

JavaScript通常用于浏览器中的客户端脚本,用于操作文档对象模型(DOM)对象。但是,DOM通常不是由JavaScript引擎提供的,而是由浏览器提供的。V8-Google Chrome也提供了DOM。但是,V8提供了ECMA标准中指定的所有数据类型、操作符、对象和函数。

node.js uses V8 and not Google Chrome.

节点。js使用的是V8而不是谷歌。

Likewise, why doesn't it have a mode to run JS in retrieved pages?

同样地,为什么在检索的页面中它没有运行JS的模式呢?

I also think we don't really need it that bad. Ryan Dahl created node.js as one man(single programmer). Maybe now he(his team) will develop this, but I was already extremely amazed by the amount of code he produced(crazy). He wanted to make non-blocking easy/efficient library , which I think he did a mighty good job at.

我也认为我们不需要那么糟糕。Ryan Dahl创建节点。作为一个人(单个程序员)。也许现在他(他的团队)会开发这个,但是我已经对他的代码(疯狂)的数量感到非常惊讶。他想让非阻塞的简易/高效的图书馆,我认为他在这方面做得很好。

But then again another developer created a module which is pretty good and actively developed(today) at https://github.com/tmpvar/jsdom.

但是,另一个开发人员创建了一个非常好的模块,并在https://github.com/tmpvar/jsdom中得到了很好的开发(今天)。

What am I not understanding about Javascript engines vs the engine in a web browser? :)

我对Javascript引擎和web浏览器引擎的理解是什么?:)

Those are different things as is hopefully clear from the quote above.

这些是不同的东西,希望从上面的引用中可以清楚地看到。

#3


6  

node.js chose not to include it in their standard library. For any functionality, there is an inevitable tradeoff between comprehensiveness, scalability, and maintainability.

节点。js选择不把它包含在他们的标准库中。对于任何功能,在全面性、可伸缩性和可维护性之间存在不可避免的折衷。

That doesn't mean it's not potentially useful. There is at least one JavaScript DOM implementation intended for NodeJS (among other CommonJS implementations).

这并不意味着它没有潜在的用处。至少有一个JavaScript DOM实现是针对node . js的(在其他的CommonJS实现中)。

#4


4  

You seem to have a flawed assumption that V8 and the DOM are inextricably related, that's not the case. The DOM is actually handled by Webkit, V8 doesn't handle the DOM, it handles Javascript calls to the DOM. Don't let this discourage you, Node.js has carved out a significant niche in the realtime server market, but don't let anybody tell you it's just for servers. Node makes it possible to build almost anything with JavaScript.

您似乎有一个错误的假设,即V8和DOM是不可分割的相关的,事实并非如此。DOM实际上是由Webkit处理的,V8不处理DOM,它处理对DOM的Javascript调用。不要因此气馁,Node。js在realtime服务器市场上开辟了一个重要的利基市场,但不要让任何人告诉你它只是针对服务器的。Node使用JavaScript构建几乎任何东西成为可能。

It is possible to do what you're talking about. For example there is the very good jsdom library if you really need access to the DOM, and node-htmlparser, there are also some really good scraping libraries that take advantage of these like apricot.

你所谈论的事情是有可能实现的。例如,如果您确实需要访问DOM和node-htmlparser,那么就有非常好的jsdom库,还有一些非常好的抓取库,它们利用了这些类似于apricot的资源。

#5


4  

This is related: There is a new project (2012) called node-webkit which tries to add DOM and a lot more from Webkit to Node. Support it!

这是相关的:有一个新的项目(2012)叫做Node - Webkit,它试图添加DOM和更多的Webkit到Node。支持它!

#6


2  

To answer your underlying question, you can use JSDom and jQuery to scrape pages in node.js: http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs

为了回答您的基本问题,您可以使用JSDom和jQuery在节点中抓取页面。js:http://blog.nodejitsu.com/jsdom-jquery-in-5-lines-on-nodejs

I've used this approach a few times myself, and it works great.

我自己用过几次这种方法,效果很好。

#7


0  

Javascript != browser. Javascript as a language is not tied to browsers; node.js is simply an implementation of Javascript that is intended for servers, not browsers. Hence no DOM.

浏览器的Javascript ! =。Javascript作为一种语言并不与浏览器绑定;节点。js只是一个Javascript的实现,它是针对服务器而不是浏览器的。因此没有DOM。

#8


0  

If you read DOM as 'linked objects immediately accessible from my script' then the answer 'it does, but it's very different from set of objects available from web document script'. The main reason is that node is 'evented I/O for V8', not 'HTML tree objects for V8'

如果您将DOM作为“链接对象”,可以立即从我的脚本中访问,那么答案是“它可以,但是它与从web文档脚本中获得的对象集非常不同”。最主要的原因是,node是“V8的I/O”,而不是“V8的HTML树对象”。

#9


0  

It seems people have answered 'why' but not how. A quick answer of how is that in a web browser, a document object is exposed (hence DOM , document object model). On windows this object is called document object. You can refer to this page and look at the methods it exposes which are for handling HTML documents like createElement. I don't use node.js or haven't done COM programming in a while but I'd imagine you could use DOM in node.js by simply calling the COM object IHTMLDocument3. Of course for other platforms like Mac OS X or Linux you would probably have to use something from their OS api. This should allow you to easily build a webpage server side using DOM, or to scrape incoming web pages.

似乎人们已经回答了“为什么”,而不是如何回答。一个快速的答案是,在web浏览器中,文档对象被公开(因此DOM,文档对象模型)。在windows上,这个对象称为文档对象。您可以参考该页面并查看它公开的方法,这些方法用于处理像createElement这样的HTML文档。我不使用节点。js或者还没有做过COM编程,但是我想你可以在node中使用DOM。通过简单地调用COM对象IHTMLDocument3。当然,对于其他平台,如Mac OS X或Linux,你可能不得不使用它们的OS api。这应该允许您使用DOM轻松构建一个网页服务器端,或者抓取传入的web页面。

#10


-1  

Node.js is for serverside programming. There is no DOM to be rendered in the server.

节点。js用于服务器端编程。在服务器中没有呈现DOM。

#11


-1  

2018 answer: mainly for historical reasons, but this may change in future.

2018年答案:主要是出于历史原因,但这可能会在未来发生变化。

Historically, very little DOM manipulation was done on the server. Addiotinally, as other answers allude, the JS stdlib and the DOM are seperate libraries - if you're using node, for, say, Unix scripting, then HTMLElement and NodeList etc aren't really relevant to that.

历史上,在服务器上做的DOM操作很少。另外,作为其他的答案,JS stdlib和DOM是分离的库——如果您使用的是node,比如Unix脚本,那么HTMLElement和NodeList等与此并不相关。

However: server-side DOM manipulation is now a very common part of delivering web apps. Web servers need to understand the structure of pages, and, if asked to render a resource as HTML, deliver HTML content that reflects the initial state of a web application. This means web apps load much faster than if the server simply delivers a stub page and has the browsers then do the work of filling in the real content. Currently this is done with JSDom and similar, but in the same way node has Request and Response objects built in, having DOM functions maintained as part of the stdlib would help with this task.

不过,服务器端DOM操作现在是交付web应用程序的一个非常常见的部分。Web服务器需要了解页面的结构,如果要求将资源呈现为HTML,则提供反映Web应用程序初始状态的HTML内容。这意味着web应用程序的加载速度要快于服务器只提供一个存根页面,并让浏览器完成填充实际内容的工作。目前,这是用JSDom和类似的方法完成的,但是在相同的方式下,node拥有构建的请求和响应对象,在stdlib中维护的DOM函数将有助于完成这个任务。

#12


-4  

1) What does it mean for it to have a D ocument O bject M odel? There's no document to represent.

1)有一个D的薪水是什么意思?没有文件可以表示。

2) You're most of the time you're not retrieving pages. You can, but most Node apps probably won't be.

2)你大部分时间都没有检索页面。你可以,但大多数节点应用程序可能不会。

3) Without a document and a browser, Javascript is just another programming language. So you may ask why there isn't a DOM in C# or Java

没有文档和浏览器,Javascript只是另一种编程语言。因此,您可能会问为什么c#或Java中没有DOM。