Skip to content

Instantly share code, notes, and snippets.

@xtina-starr
Last active August 29, 2015 14:04
Show Gist options
  • Save xtina-starr/d92164f68702c6f2a8fa to your computer and use it in GitHub Desktop.
Save xtina-starr/d92164f68702c6f2a8fa to your computer and use it in GitHub Desktop.
A node.js primer that is a work in progress

Node Primer

What is node.js?

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:

alt text

When to use node.js?

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

  1. Real-time updates like Twitter
  2. Multiple users like a chat
  3. A lot of data like with Facebook

Installing Node

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

How to create a server

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
Then navigate to localhost:1337

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment