跳到内容

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.】

变化的是生态系统。

在浏览器中,大多数情况下你所做的事情是与 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.】

阅读时间
5 min