Node.js basics
“a purely evented, non-blocking infrastructure to script highly concurrent programs”
Gergely Nemeth
Users
Gergely Nemeth
● eBay
● LinkedIn
● Yahoo
● Heroku
● Medium
● Yammer
● Trello
● etc...
Why?
Gergely Nemeth
● Uses V8
● Non-blocking
● Event-driven
● CommonJS module format
When to use it?
Gergely Nemeth
● Lots of non-blocking operations
● JSON APIs
● Reuse code across the client and the server
● Shelling out to unix tools
● Streaming data
● Crawling
When to forget it?
LongDatabaseOperation.on(‘finish’, function () {
//won’t be called!
});
while (true) {
//do some stuff
}
Gergely Nemeth
● Computationally heavy applications
When to forget it?
LongDatabaseOperation.on(‘finish’, function () {
//won’t be called!
});
while (true) {
//do some stuff
}
Gergely Nemeth
● Computationally heavy applications
1. All code runs in a single thread
2. Functions listen on events, act and sleep
How?
Gergely Nemeth
● Download from: http://nodejs.org/ (or nvm on unix-based systems)
● Hello world:
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello Worldn");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
● Run with: node app.js
NPM
Gergely Nemeth
● Node Packaged Modules
● package.json
{
"name": "MyAwesomeApplication",
"version": "0.0.0",
"dependencies": {
"nodemailer": "~0.5.2",
"express": "~3.3.4"
}
}
● npm install
● npm install XX --save
Next...
Gergely Nemeth
● Node.js best practices
● Express, Passport
● Websockets with Socket.io
● MongoDB with Mongoose
● Redis
● Hogan.js (mustache)

Node.js basics

  • 1.
    Node.js basics “a purelyevented, non-blocking infrastructure to script highly concurrent programs” Gergely Nemeth
  • 2.
    Users Gergely Nemeth ● eBay ●LinkedIn ● Yahoo ● Heroku ● Medium ● Yammer ● Trello ● etc...
  • 3.
    Why? Gergely Nemeth ● UsesV8 ● Non-blocking ● Event-driven ● CommonJS module format
  • 4.
    When to useit? Gergely Nemeth ● Lots of non-blocking operations ● JSON APIs ● Reuse code across the client and the server ● Shelling out to unix tools ● Streaming data ● Crawling
  • 5.
    When to forgetit? LongDatabaseOperation.on(‘finish’, function () { //won’t be called! }); while (true) { //do some stuff } Gergely Nemeth ● Computationally heavy applications
  • 6.
    When to forgetit? LongDatabaseOperation.on(‘finish’, function () { //won’t be called! }); while (true) { //do some stuff } Gergely Nemeth ● Computationally heavy applications 1. All code runs in a single thread 2. Functions listen on events, act and sleep
  • 7.
    How? Gergely Nemeth ● Downloadfrom: http://nodejs.org/ (or nvm on unix-based systems) ● Hello world: // Load the http module to create an http server. var http = require('http'); // Configure our HTTP server to respond with Hello World to all requests. var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello Worldn"); }); // Listen on port 8000, IP defaults to 127.0.0.1 server.listen(8000); // Put a friendly message on the terminal console.log("Server running at http://127.0.0.1:8000/"); ● Run with: node app.js
  • 8.
    NPM Gergely Nemeth ● NodePackaged Modules ● package.json { "name": "MyAwesomeApplication", "version": "0.0.0", "dependencies": { "nodemailer": "~0.5.2", "express": "~3.3.4" } } ● npm install ● npm install XX --save
  • 9.
    Next... Gergely Nemeth ● Node.jsbest practices ● Express, Passport ● Websockets with Socket.io ● MongoDB with Mongoose ● Redis ● Hogan.js (mustache)