Node.js is an event-driven programming model for server-side JavaScript. In addition to being a server, it's also a runtime environment in the same way that Perl, Python, and Ruby are. It uses the V8 JavaScript Engine developed by Google to compile JavaScript.
Highlights & Key Concept To Understand:
- Event-driven I/O
- Single-threaded
- Non-blocking
- Server-side JavaScript
- Callback Functions
- JavaScript Closures
To better understand the non-blocking, event-driven nature of node checkout this awesome article that breaks it down.
Node.js uses the event-driven I/O and handles it with an Event Loop. Event Loop is a construct that waits for and dispatches events or messages in a program. For the visual learners view the diagram below:
Node is a great tool for real-time updating, handling lots of users, and in some cases using a lot of data (not necessarily computing a lot of data).
Examples
- Real-time updates like Twitter
- Multiple users like a chat
- A lot of data like with Facebook
If you don't already have npm installed, I would suggest doing so by running the command:
curl http://npmjs.org/install.sh | sh
With the NPM you can install node with the command below:
npm install node
Pulled from the nodejs.org website, this simple web server written in Node responds with "Hello World" for every request.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
To run your server, put the code into a file server.js and execute the code from the command line:
node server.js
And there you have it. You've create a server that serves "Hello World" to the browser.
Here are some other resources to check out to learn more about node:
- Skillcrush - a highlevel article on node from skillcrush.com
- Why node.js - a deeper explanation of node.js
- node.js - evented I/O for the backend
- Express - fast node.js network app framework
- Design patterns - Intro to node.js design patterns