Javascript 为什么 Node.js 没有原生 DOM?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6657216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:39:26  来源:igfitidea点击:

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

javascriptnode.jsdomv8headless-browser

提问by PeterB

When I discovered that Node.js was built using the V8 JavaScript engine, I thought:

当我发现Node.js 是使用 V8 JavaScript 引擎构建时,我想:

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.

太好了,网页抓取会更容易,因为页面将像在浏览器中一样呈现,“原生”DOM 支持 XPath 和页面上执行的任何 AJAX 调用。

  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?
  1. 当它使用与 Chrome 相同的 JavaScript 引擎时,为什么它没有原生 DOM?
  2. 为什么它没有在检索到的页面中运行 JavaScript 的模式?
  3. 我对 JavaScript 引擎与 Web 浏览器中的引擎有什么不了解?

Many thanks!

非常感谢!

回答by Pointy

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 requirementhowever that any particular JavaScript runtime will have anyfacilities exposed via the global object.

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

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

Node.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 服务器中使用旧的基于 Rhino Java 的 JavaScript 实现。该环境也与任何 DOM 毫无关系。这是我自己的应用程序,负责用设施填充全局对象以执行我需要它能够执行的操作,而不是 DOM。

Note that there are projects like jsdomif 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.

请注意,如果您想在 Node 项目中使用虚拟 DOM ,则有像jsdom这样的项目。由于其作为服务器端平台的本质,DOM 是一种 Node 可以不用的工具,并且仍然适用于各种服务器应用程序。这并不是说 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 willhave a DOM.

对于“为什么?”这个问题,可能会有一些“官方”的答案。在那里,但这基本上只是维护 Node(现在是 Node 基金会)的人的事。如果一些勇敢的开发人员决定 Node 应该默认提供一组模块来支持虚拟 DOM,并且成功运行并实现了这一点,那么 Node拥有一个 DOM。

回答by Alfred

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

PS:在阅读这个问题时,我还想知道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 pagewhich 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 也是如此——谷歌浏览器提供了 DOM。然而,V8 确实提供了 ECMA 标准中指定的所有数据类型、运算符、对象和函数。

node.js uses V8and not Google Chrome.

node.js 使用V8而不是Google Chrome.

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

我也认为我们真的不需要那么糟糕。Ryan Dahl 作为一个人(单身程序员)创建了 node.js。也许现在他(他的团队)会开发这个,但我已经对他产生的代码量感到非常惊讶(疯狂)。他想制作一个非阻塞的简单/高效的库,我认为他在这方面做得非常好。

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.

这些是不同的事情,希望从上面的引用中可以清楚地看出。

回答by user7637745

The Document Object Model(DOM in short)is a programming interface for HTML and XML documentsand it represents the page so that programs can change the document structure, style, and content. More on this subject.

文档对象模型(DOM简称)是一个编程接口HTML和XML文件以及它所代表的页面,这样的程序可以改变文档结构,风格和内容。更多关于这个主题。



The necessary distinction between client-side (browser) and server-side (Node.js) and their main goals:

客户端(浏览器)和服务器端(Node.js)之间的必要区别及其主要目标:

  • Client-side: accessing and displaying information of the web
  • Server-side: providing stable and reliable ways to deliver web information
  • 客户端:访问和显示网页信息
  • 服务器端:提供稳定可靠的网络信息传递方式

Why is there no DOM in Node.js be default?

为什么 Node.js 中没有 DOM 是默认的?

By default, Node.js doesn't have access, nor have any knowledge about the actual DOM in your own browser. Node.js just delivers the data, that will be used by your own browser to process and render the whole website, the DOM included. The server provides the data to your browser to use and process. That is the intended way.

默认情况下,Node.js 无权访问,也不了解您自己浏览器中的实际 DOM。Node.js 只提供数据,您自己的浏览器将使用这些数据来处理和呈现整个网站,包括 DOM。服务器将数据提供给您的浏览器使用和处理。这就是预期的方式。

Why wouldn't you want to access the DOM in Node.js?

为什么不想访问 Node.js 中的 DOM?

Accessing your browser's actual DOM using Node.js would be just simply out of the goal of the server. Your own browser's role is to display the data coming from the server. However it is certainly possible and there are multiple solutions in different level of depths and varieties to pre-render, manipulate or change the DOM using AJAX calls. We'll see what future trends will bring.

使用 Node.js 访问浏览器的实际 DOM 完全超出了服务器的目标。您自己的浏览器的作用是显示来自服务器的数据。然而,这当然是可能的,并且有多种不同深度和种类的解决方案来使用 AJAX 调用预渲染、操作或更改 DOM。我们将看到未来的趋势会带来什么。

Why would you want to access the DOM in Node.js?

为什么要访问 Node.js 中的 DOM?

By default, you shouldn't access your own, actual DOM (at least some data of it)using Node.js. Client-side and server-side are separated in terms of role, functionality, and responsibility based on years of experience and knowledge. Although there are several situations, where there are solid reasons to do so:

默认情况下,您不应使用 Node.js访问自己的实际 DOM (至少是其中的一些数据)。基于多年的经验和知识,客户端和服务器端在角色、功能和责任方面是分开的。虽然有几种情况,但有充分的理由这样做:

  • Gathering usage data(A/B testing, UI/UX efficiency and feedback)
  • Headless testing(Development, automation, web-scraping)
  • 收集使用数据(A/B 测试、UI/UX 效率和反馈)
  • 无头测试(开发、自动化、网络抓取)

How can you access the DOM in Node.js?

如何访问 Node.js 中的 DOM?

  • jsdom: pure-JavaScript implementation, good for testing your own DOM/browser-related project
  • cheerio: great solution if you like/often use jQuery
  • puppeteer: Google's own way to provide headless testing using Google Chrome
  • own solution(your possible future project link here)
  • jsdom: 纯 JavaScript 实现,适合测试你自己的 DOM/浏览器相关项目
  • Cheerio:如果您喜欢/经常使用 jQuery,那么很好的解决方案
  • puppeteer:谷歌自己的方式使用谷歌浏览器提供无头测试
  • 自己的解决方案(您可能的未来项目链接在这里)


Although these solutions do not provide a way to access your browser's own, actual DOM by default, but you can create a project to send some form of data about your DOMto the server, then use/render/manipulate that data based on your needs.

尽管默认情况下这些解决方案没有提供访问浏览器自己的实际 DOM 的方法,但是您可以创建一个项目将有关 DOM 的某种形式的数据发送到服务器,然后根据您的需要使用/渲染/操作该数据.

...and yes, web-scraping and web development in terms of tools and utilities became more sophisticated and certainly easier in several fields.

...是的,在工具和实用程序方面的网络抓取和网络开发变得更加复杂,并且在多个领域肯定更容易。

回答by Matthew Flaschen

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

node.js 选择不将其包含在他们的标准库中。对于任何功能,在全面性、可扩展性和可维护性之间都存在不可避免的权衡。

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

这并不意味着它没有潜在的用处。至少有一个用于 NodeJS 的JavaScript DOM实现(以及其他 CommonJS 实现)。

回答by Omni5cience

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 已经在实时服务器市场上开辟了一个重要的利基市场,但不要让任何人告诉您它只是用于服务器。Node 使使用 JavaScript 构建几乎任何东西成为可能。

It is possible to do what you're talking about. For example there is the very good jsdomlibrary 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,则有非常好的jsdom库和node-htmlparser,还有一些非常好的抓取库,它们利用了这些,例如apricot

回答by Ciantic

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

这是相关的:有一个名为node-webkit的新项目(2012 年),它试图将 DOM 和更多内容从 Webkit 添加到 Node。支持它!

回答by Dave Ward

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 在 node.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.

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

回答by Paul Sonier

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 作为一种语言与浏览器无关;node.js 只是针对服务器而非浏览器的 Javascript 实现。因此没有DOM。

回答by wba0

Node is a runtime environment, it does not render a DOM like a browser.

Node 是一个运行时环境,它不像浏览器那样渲染 DOM。

回答by samanime

Because there isn't a DOM. DOM stands for Document Object Model. There is no document in Node, so not DOM to manipulate it. That is definitively a browser thing.

因为没有 DOM。DOM 代表Document Object Model. Node 中没有文档,所以没有 DOM 来操作它。这绝对是浏览器的事情。

You can use a library like cheeriothough which gives you some simple DOM manipulation.

您可以使用像cheerio虽然这样的库,它可以为您提供一些简单的 DOM 操作。

Node is server-level JavaScript. It's just the language applied to a basic system API, more like C++ or Java.

Node 是服务器级的 JavaScript。它只是应用于基本系统 API 的语言,更像是 C++ 或 Java。