Node.js 在 Web 开发环境中适合什么位置?
我知道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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单来说,node.js 服务器是 Apache Web 服务器之类的替代品,但它主要是用 JavaScript 编写的,运行在服务器上(由 V8 引擎执行)而不是在服务器上运行。客户端。它可以通过封装在 JavaScript 接口中的“本机代码”模块(例如用 C++ 编写)来扩展以添加功能,但据我所知,大多数 Node.js 模块都是纯 JavaScript。
“事件驱动的 I/O”只是一个描述 JavaScript 中常用的异步回调机制的术语。在 Node.js 中,您为各种事物提供回调,并且当相关事件发生时调用您的函数。
根据您添加的模块数量,node.js 服务器与 Apache 之类的服务器相比相对轻量级,并且在某些方面更简单。
我认为 Node.js 的两个主要优点是:
这是我刚刚看到的一篇文章,可能也能提供一些启发: 什么是 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:
Here's an article I just came across that might also shed some light: What is Node.js?
虽然我无法对 @sje 所说的内容添加太多内容,但我将重复他分享的博客链接,因为这是我发现的快速解释 Nodejs 的最佳资源:
http://radar.oreilly.com/2011/07/what-is-node.html
另请注意它来自 OReilly,我们大多数人都知道它是市场上程序员最佳参考文献的出版商;)
这是完全错误的。这是您可能做出的关于节点的最错误的假设。 Node 在服务器上运行 javascript,就像运行 ruby、php 或 asp.net 代码一样。浏览器也可以运行 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 ;)
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.
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.
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:
The same place ruby and php and perl and python and asp.net do. On the server, generating code that the client receives.
我还没有看到有人对此给出简单的答案。
Node.js 是:
值得注意的是,Node 也不一定必须用于 Web 开发。它的目的是“事件IO”。
I haven't seen anyone give a simple answer to this yet.
Node.js is:
It's important to note, Node doesn't necessarily have to be used for web development either. It's purpose is, "evented IO".