7

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

Benny Tjia
  • 4,853
  • 10
  • 39
  • 48
  • From what I understand, node.js can handle a ton of concurrent requests, because each request does not spawn a new thread, like Apache would do. Beyond that, I can't wait to read some of the answers to this question. – Stephen Jul 09 '11 at 04:04
  • It's just like PHP at it's beginning it was only a small set of functions/utlities written in C useful to serve files and other web stuff http://groups.google.com/group/comp.infosystems.www.authoring.cgi/msg/cc7d43454d64d133?pli=1. The advantage is that JS is more flexible than C++, and close to C++ speed using te V8 engine. Non blocking, etc.. – zad Jul 09 '11 at 04:13
  • Its a low level framework for server side app development, uses javascript and has all the advantages you probably read about in wikipedia. There are frameworks that sit on top of node.js – zad Jul 09 '11 at 04:21
  • The fact that node.js is using javascript is really not important because very little code in practice is shared. The advantage of using javascript is that it is an "easy" and "fun" language to write. – Matthew FL Jul 09 '11 at 04:22
  • @MatthewFL that is not the advantage to using javascript. It's because it's a powerful language that supports many paradigms naturally. – jcolebrand Jul 09 '11 at 05:20

3 Answers3

9

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?

sje397
  • 41,293
  • 8
  • 87
  • 103
  • 1
    The second point isn't exactly valid, since most people who claim to know JS don't actually know the nuances of the language; they just know jQuery and DOM manipulation. One of the main reasons why JS is such a good choice for Node is that it natively supports asynchronous, **non-blocking** I/O. – Steve Wang Jul 09 '11 at 04:25
  • @Steve Wang: Is that part of the JS spec, or due to the V8 implementation? – sje397 Jul 09 '11 at 04:28
  • That's part of the spec. Javascript has always supported asynchronous non-blocking I/O. What it has not supported is the evented IO that node does, that's what node adds to the mix. – jcolebrand Jul 09 '11 at 05:19
  • I think the 'typical' JS developer is evolving faster than Steve might thanks to some of the more active superstars of the community but otherwise I agree with him 100%. The things to get excited about JavaScript on the back end are about how it works on a deeper level, not that we don't have to learn anything new. Sharing generic utility libraries on front and back is certainly a plus though. I'm still excited about Django but I might just steal the stuff I like and move it into node.js – Erik Reppen Aug 11 '11 at 15:19
3

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.

Community
  • 1
  • 1
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
1

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

chjj
  • 14,322
  • 3
  • 32
  • 24