3. Background
node.js runs on V8. It is a set of bindings to the V8
JavaScript VM.
V8 is an open source JavaScript engine developed by
Google. Its written in C++ and is used in Google Chrome
Browser.
Latest version is v0.10.18
Is Open Source. It runs well on Linux/Unix systems, can also
run on Windows systems.
4. Installation
node.js official site:
http://nodejs.org/download/
Url: https://github.com/joyent/node/wiki
/Installing-Node.js-via-package-manager
sudo apt-get update
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs=0.10.18-1chl1~precise1
On winodws, you can use node.js installer.
7. How to GET
?
Using http.get(options, callback)
Example:
http.get("http://www.google.com/index.html", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
8. Simple http Server
http.createServer(function(request, response){ ... });
http.createServer([requestListener])
Returns a new web server object.
The requestListener is a function which is automatically
added to the 'request' event.
Class: http.Server
This is an EventEmitter with the following events:
Event: 'request'
function (request, response) { }
Emitted each time there is a request.
9. Alternate Syntax
var server = http.createServer();
server.on('request', function(request, response){ ... });
Event: 'close'
server.on('close', function(){ ... });
12. Express
Sinatra inspired web development framework
for Node.js
It is insanely fast, flexible, and simple.
Easy route URLs to callbacks
Environment based configuration
Middleware via connect – bodyParser, cookieparser etc
Install as:
npm install express
Or
sudo npm install -g express
13. REST url in Express
Let assumes there is 'user' resource.
// List
app.get('/users', function(req, res) {
});
// Create
app.post('/users', function(req, res) {
});
// Read
app.get('/users/:id', function(req, res) {
});
// Update
app.put('/users/:id', function(req, res) {
});
// Delete
app.del('/users/:id', function(req, res) {
});
14. Express simple example
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
24. Express Middleware
cookieParser():
Parses the Cookie header field and populates
'req.cookies' with an object keyed by the
cookie names.
Usage:
app.use(express.cookieParser());
or
app.use(express.cookieParser('some secret'));
26. Express Middleware
methodOverride():
To simulate DELETE and PUT.
Use when _method post parameter set to 'delete' or
'put'.
Usage:
app.use(express.methodOverride());
Client side code:
<form> ...
<input type="hidden" name="_method"
value="put" />
</form>
27. Express Middleware
session():
Setup session store with the given options.
Usage:
app.use(express.session());
Example:
app.use(express.cookieParser());
app.use(express.session({ secret: 'keyboard cat', key:
'sid', cookie: { secure: true }})
28. Express App Generation
Command to generate express app:
express --sessions --css stylus --ejs myapp
It generates an application with EJS, Stylus,
and session.
EJS: Embedded JavaScript template engine
- EJS cleans the HTML out of
your JavaScript with client side templates.
- Similar to ERB in ruby.
30. Yahoo! : iPad App Livestand uses Yahoo! Manhattan
framework which is based on Node.js.
LinkedIn : LinkedIn uses a combination of Node.js and
MongoDB for its mobile platform. iOS and Android apps
are based on it.
eBay : Uses Node.js along with ql.io to help application
developers in improving eBay’s end user experience.
Dow Jones : The WSJ Social front-end is written
completely in Node.js, using Express.js, and many other
modules.
Complete list can be found at:
https://github.com/joyent/node/wiki/Projects,-
Applications,-and-Companies-Using-Node
Who is using Node.js?