Node.js 在 Web 开发环境中适合什么位置?

发布于 2024-11-19 02:49:06 字数 828 浏览 2 评论 0原文

我知道node.js据说是“事件驱动的I/O”托管在V8 Javascript引擎上的服务器端JavaScript。我访问了 node.js 网站,然后阅读了维基百科条目,但无法完全了解在哪里使用它以及它如何有用。 “事件驱动 IO”? “V8 Javascript 引擎”? 但在某些情况下,我发现使用“服务器端”javascript 有点矫枉过正..我以 node.js 的维基百科条目

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8000);

console.log('Server running at http://127.0.0.1:8000/');

我一直在想,运行一个服务器真的有一个重要的目的吗?特别是提供 javascript 文件在应用程序的前端部分执行?

我还在 github 中分叉了 node.js 存储库以了解有关其工作原理的更多信息,结果发现它的一些模块是用 C++ 编写的。那么它毕竟不是 javascript 吗?

有人能给我一个清楚的解释吗?抱歉,如果问题不清楚或什么的,我只是一个初学者。将不胜感激任何意见/建议。谢谢

I know that node.js is said to be "event-driven I/O" server-side javascript hosted on V8 Javascript engine. I visited the node.js website, and then read the wikipedia entry, but cant quite get the whole idea of where to use it and how it will be useful. "Event-driven I-O"? "V8 Javascript engine"? In some context though, I see that using "server-side" javascript as a little overkill..I take for instance this piece of code in the wikipedia entry of node.js:

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8000);

console.log('Server running at http://127.0.0.1:8000/');

I have been thinking, is there really a significant purpose in running a server that particularly serves javascript files that are executed in the front-end part of the application?

I also forked the node.js repo in github to learn more about how it works, and it turns out that some of its modules are written in C++. So it is not a javascript after all then?

Can somebody give me a clear explanation about all this? Sorry if the question is not clear or something, I am just a beginner. Will appreciate any input/suggestions. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靖瑶 2024-11-26 02:49:07

简单来说,node.js 服务器是 Apache Web 服务器之类的替代品,但它主要是用 JavaScript 编写的,运行在服务器上(由 V8 引擎执行)而不是在服务器上运行。客户端。它可以通过封装在 JavaScript 接口中的“本机代码”模块(例如用 C++ 编写)来扩展以添加功能,但据我所知,大多数 Node.js 模块都是纯 JavaScript。

“事件驱动的 I/O”只是一个描述 JavaScript 中常用的异步回调机制的术语。在 Node.js 中,您为各种事物提供回调,并且当相关事件发生时调用您的函数。

根据您添加的模块数量,node.js 服务器与 Apache 之类的服务器相比相对轻量级,并且在某些方面更简单。

我认为 Node.js 的两个主要优点是:

  1. 它允许您用同一种语言编写 Web 应用程序的服务器端和客户端部分。在某些情况下,您可以在两侧使用相同的代码。
  2. 它使所有了解 JavaScript 的 Web 开发人员都可以访问服务器端编码,而无需学习 PHP 或 Java 等更常见的服务器端语言。

这是我刚刚看到的一篇文章,可能也能提供一些启发: 什么是 Node .js?

The node.js server is, in simple terms, a replacement for something like the Apache web server - but it is largely written in JavaScript which runs on the server (executed by the V8 engine) rather than on the client side. It can be extended with 'native code' modules (written in e.g. C++) wrapped in a JavaScript interface to add functionality, but AFAIK most node.js modules are pure JavaScript.

"Event driven I/O" is just a term that describes the normal asynchronous callback mechanisms you're used to in JavaScript. In node.js you supply callbacks for all sorts of things, and your function is called when the relevant event occurs.

Depending on how many modules you add, a node.js server is relatively lightweight compared to something like Apache, and in some ways a lot simpler.

The two main advantages to node.js I see are:

  1. It allows you to write both the server-side and client-side parts of a web application in the same language. In some cases you can use the same code on both sides.
  2. It makes server-side coding accessible to all those web developers out there that know JavaScript without having to learn a more common server-side language like PHP or Java.

Here's an article I just came across that might also shed some light: What is Node.js?

作死小能手 2024-11-26 02:49:07

虽然我无法对 @sje 所说的内容添加太多内容,但我将重复他分享的博客链接,因为这是我发现的快速解释 Nodejs 的最佳资源:

http://radar.oreilly.com/2011/07/what-is-node.html

另请注意它来自 OReilly,我们大多数人都知道它是市场上程序员最佳参考文献的出版商;)

我一直在想,运行一个专门为在应用程序前端部分执行的 JavaScript 文件提供服务的服务器真的有重要的目的吗?

这是完全错误的。这是您可能做出的关于节点的最错误的假设。 Node 在服务器上运行 javascript,就像运行 ruby​​、php 或 asp.net 代码一样。浏览器也可以运行 javascript 的事实与节点无关。

当然,您可以在服务器和客户端之间共享模块(例如,表单数据的验证例程),但总的来说,代码库是不同的,因为它们用于不同的事情。

我还在 github 中分叉了 node.js 存储库以了解有关其工作原理的更多信息,结果发现它的一些模块是用 C++ 编写的。那么它毕竟不是 javascript 吗?

是的,node是一个使用V8引擎解释javascript的服务器。它必须被写在某些东西中。我会给你一个比较:Microsoft .NET 代码大部分是在 .NET 之上用 .NET 编写的,但实际完成工作的主要代码是管理托管的运行时(大多数人称之为 CLR)。 -code,该代码是用 C 编写的。节点也是如此。是的,其中大部分(如您所见)是用 javascript 编写的,但运行其他所有内容的核心库是用 C 编写的。

有人能给我一个清楚的解释吗?抱歉,如果问题不清楚或什么的,我只是一个初学者。将不胜感激任何意见/建议。谢谢

我希望这有助于部分解决这个问题。有很多内容需要介绍,并且无需深入了解 evented-io(其中涉及理解进程和线程以及 io 访问和许多其他内容),这几乎是这个问题的基本高级答案。如果您愿意,我邀请您来到聊天服务器上的 Nodejs 房间,进行更多流畅的随机讨论。 https://chat.stackoverflow.com/rooms/642/node-js

关于第一个问题你问:

它适合什么地方?

ruby、php、perl、python 和 asp.net 也是如此。在服务器上,生成客户端接收的代码。

While I can't add much to what @sje said, I will repeat that blog link he shared, as that is the best resource that I've found to explain nodejs quickly:

http://radar.oreilly.com/2011/07/what-is-node.html

Also note that it's from OReilly, who most of us know as being the publisher of the best references for programmers on the market in general ;)

I have been thinking, is there really a significant purpose in running a server that particularly serves javascript files that are executed in the front-end part of the application?

This is totally wrong. This is the most wrong assumption about node you could make. Node runs the javascript on the server much as ruby or php or asp.net code is run. The fact that the browser can also run javascript has no bearing on node.

Granted, you can share modules between the server and the client (for instance, validation routines for form data) but by and large the codebases are different as they are intended for different things.

I also forked the node.js repo in github to learn more about how it works, and it turns out that some of its modules are written in C++. So it is not a javascript after all then?

Yes, node is a server that interprets javascript using the V8 engine. It has to be written in something. I'll give you a comparison: Microsoft .NET code is mostly written in .NET on top of .NET, but the main code that actually does the work, the runtime (the CLR as most people refer to it) that manages the managed-code, that code is written in C. The same thing with node. Yes, most of it (as you saw) is written in javascript, but the core libraries that run everything else are written in C.

Can somebody give me a clear explanation about all this? Sorry if the question is not clear or something, I am just a beginner. Will appreciate any input/suggestions. thanks

I hope this helped clear it up in part. There's a lot to cover, and without going into evented-io (which involves understanding processes and threads and io access and a lot of other stuff) this is pretty much the basic high-level answer to this question. I invite you to the nodejs room on the chat server here if you like, for more random discussions that are fluid. https://chat.stackoverflow.com/rooms/642/node-js

As to the first question you asked:

Where does it fit in?

The same place ruby and php and perl and python and asp.net do. On the server, generating code that the client receives.

泛泛之交 2024-11-26 02:49:07

我还没有看到有人对此给出简单的答案。

Node.js 是:

  • v8 javascript 引擎
  • 事件循环
  • 一些 C++ 绑定,除其他外,它们提供了 v8 IO 功能(网络和文件 IO)

值得注意的是,Node 也不一定必须用于 Web 开发。它的目的是“事件IO”。

I haven't seen anyone give a simple answer to this yet.

Node.js is:

  • the v8 javascript engine
  • an event loop
  • some c++ bindings, among other things, that give v8 IO capabilities (both network and file IO)

It's important to note, Node doesn't necessarily have to be used for web development either. It's purpose is, "evented IO".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文