Node.js 和浏览器之间的差异

¥Differences between Node.js and the Browser

浏览器和 Node.js 都使用 JavaScript 作为编程语言。构建在浏览器中运行的应用与构建 Node.js 应用完全不同。尽管它始终是 JavaScript,但仍存在一些关键差异,这些差异使体验截然不同。

¥Both the browser and Node.js use JavaScript as their programming language. Building apps that run in the browser is completely different from building a Node.js application. Despite the fact that it's always JavaScript, there are some key differences that make the experience radically different.

从广泛使用 JavaScript 的前端开发者的角度来看,Node.js 应用带来了巨大的优势:编程一切的舒适感 - 前端和后端 - 使用单一语言。

¥From the perspective of a frontend developer who extensively uses JavaScript, Node.js apps bring with them a huge advantage: the comfort of programming everything - the frontend and the backend - in a single language.

你有一个巨大的机会,因为我们知道全面深入地学习一门编程语言有多难,并且通过使用相同的语言在网络上执行所有工作 - 无论是在客户端还是在服务器上,你都处于独特的优势地位。

¥You have a huge opportunity because we know how hard it is to fully, deeply learn a programming language, and by using the same language to perform all your work on the web - both on the client and on the server, you're in a unique position of advantage.

改变的是生态系统。

¥What changes is the ecosystem.

在浏览器中,你所做的大部分时间都是与 DOM 或其他 Web 平台 API(如 Cookies)进行交互。当然,Node.js 中不存在这些。你没有浏览器提供的 documentwindow 和所有其他对象。

¥In the browser, most of the time what you are doing is interacting with the DOM, or other Web Platform APIs like Cookies. Those do not exist in Node.js, of course. You don't have the document, window and all the other objects that are provided by the browser.

在浏览器中,我们没有 Node.js 通过其模块提供的所有好 API,例如文件系统访问功能。

¥And in the browser, we don't have all the nice APIs that Node.js provides through its modules, like the filesystem access functionality.

另一个很大的区别是,在 Node.js 中你可以控制环境。除非你正在构建任何人都可以部署到任何地方的开源应用,否则你知道将在哪个版本的 Node.js 上运行该应用。与浏览器环境相比,你无法选择访问者将使用哪种浏览器,这非常方便。

¥Another big difference is that in Node.js you control the environment. Unless you are building an open source application that anyone can deploy anywhere, you know which version of Node.js you will run the application on. Compared to the browser environment, where you don't get the luxury to choose what browser your visitors will use, this is very convenient.

这意味着你可以编写 Node.js 版本支持的所有现代 ES2015+ JavaScript。由于 JavaScript 移动速度如此之快,但浏览器升级速度可能有点慢,有时在网络上你只能使用较旧的 JavaScript/ECMAScript 版本。你可以使用 Babel 将你的代码转换为与 ES5 兼容,然后再将其发送到浏览器,但在 Node.js 中,你不需要这样做。

¥This means that you can write all the modern ES2015+ JavaScript that your Node.js version supports. Since JavaScript moves so fast, but browsers can be a bit slow to upgrade, sometimes on the web you are stuck with using older JavaScript / ECMAScript releases. You can use Babel to transform your code to be ES5-compatible before shipping it to the browser, but in Node.js, you won't need that.

另一个区别是 Node.js 同时支持 CommonJS 和 ES 模块系统(自 Node.js v12 以来),而在浏览器中,我们开始看到 ES 模块标准正在实现。

¥Another difference is that Node.js supports both the CommonJS and ES module systems (since Node.js v12), while in the browser, we are starting to see the ES Modules standard being implemented.

实际上,这意味着你可以在 Node.js 中同时使用 require()import,而在浏览器中则仅限于 import

¥In practice, this means that you can use both require() and import in Node.js, while you are limited to import in the browser.