Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Node.js have a native DOM?

When I discovered that Node.js was built using 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 supporting XPath and any AJAX calls on the page executed.

  1. Why doesn't it have a native DOM when it uses the same JavaScript engine as Chrome?
  2. Why doesn't it have a mode to run JavaScript in retrieved pages?
  3. What am I not understanding about JavaScript engines vs the engine in a web browser?

Many thanks!

like image 675
PeterB Avatar asked Jul 11 '11 22:07

PeterB


People also ask

Does NodeJS have DOM?

DOM is not a browser thing, it's an API for working with XML/HTML/SGML-like documents, that's all. The reason Node. js doesn't have it simply because their primary scope was "backend" services, and DOM parsing evidently isn't an API they considered essential there.

What is DOM node JS?

The "DOM" is a tree structure that represents the HTML of the website, and every HTML element is a "node". See Document Object Model (DOM). More specifically, "Node" is an interface that is implemented by multiple other objects, including "document" and "element".

How does node have event driven programming language feature?

Event-Driven Programming Node. js uses events heavily and it is also one of the reasons why Node. js is pretty fast compared to other similar technologies. As soon as Node starts its server, it simply initiates its variables, declares functions and then simply waits for the event to occur.


2 Answers

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.

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.

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.

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.

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.

like image 195
Pointy Avatar answered Sep 29 '22 13:09

Pointy


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

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

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

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.

node.js uses V8 and not Google Chrome.

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

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 a non-blocking easy/efficient library, which I think he did a mighty good job at.

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

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

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

like image 33
Alfred Avatar answered Sep 29 '22 14:09

Alfred