Jef*_*eff 506 javascript v8 evented-io node.js
我没有完全了解Node.js的全部内容.也许是因为我主要是一个基于Web的业务应用程序开发人员.它是什么以及它的用途是什么?
到目前为止,我的理解是:
我的理解是否正确?如果是,那么即使I/O有什么好处,它对于并发性的东西更多吗?另外,Node.js的方向是成为一个类似于基于JavaScript(V8的)编程模型的框架吗?
Dav*_*son 619
我在工作中使用Node.js,并发现它非常强大.被迫选择一个词来描述Node.js,我会说"有趣"(这不是一个纯正的形容词).社区充满活力,不断发展壮大.尽管JavaScript很奇怪,但它可以成为一种很好的编码语言.而且你每天都会重新思考自己对"最佳实践"和结构良好的代码模式的理解.现在有很多想法流入Node.js,并且在其中工作会让你接触到所有这些想法 - 伟大的精神举重.
生产中的Node.js绝对是可能的,但远不是文档似乎承诺的"交钥匙"部署.使用Node.js v0.6.x,"cluster"已经集成到平台中,提供了一个必不可少的构建块,但是我的"production.js"脚本仍然是〜150行逻辑来处理诸如创建日志之类的东西对于"严肃"的生产服务,您还需要准备好限制传入的连接并完成Apache为PHP所做的所有事情.为了公平起见,Ruby on Rails的有这个确切的问题.它通过两种互补机制解决:1)将Ruby放在Rails/Node上.Apache/Lighttd).Web服务器可以有效地提供静态内容,访问日志记录,重写URL,终止SSL,实施访问规则以及管理多个子服务.对于访问实际节点服务的请求,Web服务器代理请求.2)使用像Unicorn这样的框架来管理工作进程,定期回收它们等等.我还没有找到一个看似完全烘焙的Node.js服务框架; 它可能存在,但我还没有找到它,仍然在我的手工卷"production.js"中使用~150行.
Reading frameworks like Express makes it seem like the standard practice is to just serve everything through one jack-of-all-trades Node.js service ... "app.use(express.static(__dirname + '/public'))". For lower-load services and development, that's probably fine. But as soon as you try to put big time load on your service and have it run 24/7, you'll quickly discover the motivations that push big sites to have well baked, hardened C-code like Nginx fronting their site and handling all of the static content requests (...until you set up a CDN, like Amazon CloudFront)). For a somewhat humorous and unabashedly negative take on this, see this guy.
Node.js is also finding more and more non-service uses. Even if you are using something else to serve web content, you might still use Node.js as a build tool, using npm modules to organize your code, Browserify to stitch it into a single asset, and uglify-js to minify it for deployment. For dealing with the web, JavaScript is a perfect impedance match and frequently that makes it the easiest route of attack. For example, if you want to grovel through a bunch of JSON response payloads, you should use my underscore-CLI module, the utility-belt of structured data.
有关JavaScript和Node.js的另一个视角,请查看从Java到Node.js,这是一篇关于Java开发人员学习Node.js的印象和经验的博客文章.
模块 在考虑节点时,请记住,您选择的JavaScript库将定义您的体验.大多数人使用至少两个,一个异步模式助手(Step,Futures,Async)和一个JavaScript糖模块(Underscore.js).
助手/ JavaScript糖:
异步模式模块:
或者阅读有关异步库的所有内容,请参阅此面板 -与作者的访谈.
Web框架:
Testing:
Also, check out the official list of recommended Node.js modules. However, GitHub's Node Modules Wiki is much more complete and a good resource.
To understand Node, it's helpful to consider a few of the key design choices:
Node.js is EVENT BASED and ASYNCHRONOUS/NON-BLOCKING. Events, like an incoming HTTP connection will fire off a JavaScript function that does a little bit of work and kicks off other asynchronous tasks like connecting to a database or pulling content from another server. Once these tasks have been kicked off, the event function finishes and Node.js goes back to sleep. As soon as something else happens, like the database connection being established or the external server responding with content, the callback functions fire, and more JavaScript code executes, potentially kicking off even more asynchronous tasks (like a database query). In this way, Node.js will happily interleave activities for multiple parallel workflows, running whatever activities are unblocked at any point in time. This is why Node.js does such a great job managing thousands of simultaneous connections.
Why not just use one process/thread per connection like everyone else? In Node.js, a new connection is just a very small heap allocation. Spinning up a new process takes significantly more memory, a megabyte on some platforms. But the real cost is the overhead associated with context-switching. When you have 10^6 kernel threads, the kernel has to do a lot of work figuring out who should execute next. A bunch of work has gone into building an O(1) scheduler for Linux, but in the end, it's just way way more efficient to have a single event-driven process than 10^6 processes competing for CPU time. Also, under overload conditions, the multi-process model behaves very poorly, starving critical administration and management services, especially SSHD (meaning you can't even log into the box to figure out how screwed it really is).
Node.js is SINGLE THREADED and LOCK FREE. Node.js, as a very deliberate design choice only has a single thread per process. Because of this, it's fundamentally impossible for multiple threads to access data simultaneously. Thus, no locks are needed. Threads are hard. Really really hard. If you don't believe that, you haven't done enough threaded programming. Getting locking right is hard and results in bugs that are really hard to track down. Eliminating locks and multi-threading makes one of the nastiest classes of bugs just go away. This might be the single biggest advantage of node.
But how do I take advantage of my 16 core box?
Two ways:
Node.js lets you do some really powerful things without breaking a sweat. Suppose you have a Node.js program that does a variety of tasks, listens on a TCP port for commands, encodes some images, whatever. With five lines of code, you can add in an HTTP based web management portal that shows the current status of active tasks. This is EASY to do:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(myJavascriptObject.getSomeStatusInfo());
}).listen(1337, "127.0.0.1");
Run Code Online (Sandbox Code Playgroud)
Now you can hit a URL and check the status of your running process. Add a few buttons, and you have a "management portal". If you have a running Perl/Python/Ruby script, just "throwing in a management portal" isn't exactly simple.
But isn't JavaScript slow/bad/evil/spawn-of-the-devil? JavaScript has some weird oddities, but with "the good parts" there's a very powerful language there, and in any case, JavaScript is THE language on the client (browser). JavaScript is here to stay; other languages are targeting it as an IL, and world class talent is competing to produce the most advanced JavaScript engines. Because of JavaScript's role in the browser, an enormous amount of engineering effort is being thrown at making JavaScript blazing fast. V8 is the latest and greatest javascript engine, at least for this month. It blows away the other scripting l
pos*_*ist 213
我认为其优点是:
在VM上以动态语言(JavaScript)进行Web开发,速度非常快(V8).它比Ruby,Python或Perl快得多.
能够以最小的单个进程开销处理数千个并发连接.
JavaScript非常适合具有第一类函数对象和闭包的事件循环.人们已经知道如何使用它在浏览器中使用它来响应用户发起的事件.
很多人已经了解JavaScript,甚至是那些自称不是程序员的人.它可以说是最流行的编程语言.
在Web服务器和浏览器上使用JavaScript可以减少两个编程环境之间的阻抗不匹配,这两个编程环境可以通过JSON传递数据结构,这两个方程在方程式的两侧都是相同的.可以在服务器和客户端等之间共享重复的表单验证代码.
rfu*_*duk 85
V8是JavaScript的一个实现.它允许您运行独立的JavaScript应用程序(以及其他内容).
Node.js只是为V8编写的库,它实现了I/O. 这个概念有点难以解释,我确信有人会回答比我更好的解释...要点是,不要做一些输入或输出并等待它发生,你只是不要等待为了它完成.例如,请求文件的最后编辑时间:
// Pseudo code
stat( 'somefile' )
Run Code Online (Sandbox Code Playgroud)
这可能需要几毫秒,或者可能需要几秒钟.使用事件I/O,您只需触发请求,而不是等待您附加一个在请求完成时运行的回调:
// Pseudo code
stat( 'somefile', function( result ) {
// Use the result here
} );
// ...more code here
Run Code Online (Sandbox Code Playgroud)
这使它很像浏览器中的JavaScript代码(例如,具有Ajax样式功能).
有关更多信息,您应该查看文章Node.js真正令人兴奋,这是我对库/平台的介绍...我发现它非常好.
两个很好的例子是关于如何管理模板和使用渐进增强功能.您只需要一些轻量级的JavaScript代码即可使其完美运行.
我强烈建议您观看和阅读这些文章:
选择任何语言并尝试记住如何管理HTML文件模板以及在DOM结构中更新单个CSS类名称时必须执行的操作(例如,用户单击菜单项并且您希望将其标记为"选中"并更新页面内容).
使用Node.js就像在客户端JavaScript代码中一样简单.获取您的DOM节点并将CSS类应用于该节点.获取您的DOM节点和innerHTML您的内容(您将需要一些额外的JavaScript代码来执行此操作.阅读文章以了解更多信息).
另一个很好的例子是,您可以使用相同的代码打开或关闭JavaScript,使您的网页兼容.想象一下,您有一个用JavaScript制作的日期选择,允许您的用户使用日历获取任何日期.您可以编写(或使用)相同的JavaScript代码,以使其与您打开或关闭JavaScript一起使用.
有一个非常好的快餐店类比,最能说明Node.js的事件驱动模型,请参阅完整的文章,Node.js,Doctor's Offices和Fast Food Restaurants - 了解事件驱动的编程
以下是摘要:
如果快餐联合会遵循传统的基于线程的模式,您可以订购食物并排队等候直到收到它.在您的订单完成之前,您身后的人将无法订购.在事件驱动的模型中,您订购食物然后离开等待.然后其他人都可以自由订购.
Node.js是事件驱动的,但大多数Web服务器都是基于线程的.York解释了Node.js的工作原理:
您使用Web浏览器在Node.js Web服务器上发出"/about.html"请求.
Node.js服务器接受您的请求并调用函数从磁盘检索该文件.
当Node.js服务器正在等待检索文件时,它会为下一个Web请求提供服务.
检索文件时,会有一个插入Node.js服务器队列的回调函数.
Node.js服务器执行该功能,在这种情况下,该功能将呈现"/about.html"页面并将其发送回您的Web浏览器."