On Github geekingfrog / nodejs-intro
Slides on github and speaker deck
In a file: actor.js
function Actor() { this.name = 'Brad Pitt' } Actor.prototype.getName = function() { return this.name; } module.exports.Actor = Actor;
In a file: main.js
var Actor = require('./actor').Actor;
var express = require('express'); // no `./` here var crypto = require('crypto'); // native node.js api
Read a file
// native node module to access the file system var fs = require('fs'); try { var fileContent = fs.readFileSync('./myFile.txt'); } catch(err) { console.log('Error while reading file:', err); }
Blocking code (and there is only one thread)
var fs = require('fs'); fs.readFile('./myFile.txt', function callback(err, file) { if(err) { console.log('Got an error:', err); } else { console.log('Got file here'); } });
var fs = require('fs'); console.log('about to read file'); var result = fs.readFile('./myFile.txt', function(err, file) { if(err) console.log('error:', err); else console.log('got file'); }); console.log('done reading file?'); console.log('result:', result);
Execution result:
try { setTimeout(function() { // another async function throw new Error('catch me if you can'); }, 0); } catch(err) { console.log('Got you:', err); }
Result:
function callback(err, arg1, arg2, args...) { // error object is always the first argument } doSomethingAsync('foo', 'bar', 'baz', callback); // the callback function is always the last argument
(process.on('uncaughtException', handler));
var fs = require('fs'); fs.readFile('./myFile.txt', function callback(err, file) { if(err) { console.log('Got an error:', err); } else { console.log('Got file here'); } });
{ "name": "reveal.js", "version": "2.6.2", "description": "The HTML Presentation Framework", "homepage": "http://lab.hakim.se/reveal-js", "subdomain": "revealjs", "scripts": { "test": "grunt test", "start": "" }, "author": { "name": "Hakim El Hattab", "email": "hakim.elhattab@gmail.com", "web": "http://hakim.se" }, "repository": { "type": "git", "url": "git://github.com/hakimel/reveal.js.git" }, "engines": { "node": "~0.8.0" }, "dependencies": { "underscore": "~1.5.1", "express": "~2.5.9", "mustache": "~0.7.2", "socket.io": "~0.9.13" }, "devDependencies": { "grunt-contrib-qunit": "~0.2.2", "grunt-contrib-jshint": "~0.6.4", "grunt-contrib-cssmin": "~0.4.1", "grunt-contrib-uglify": "~0.2.4", "grunt-contrib-watch": "~0.5.3", "grunt-contrib-sass": "~0.5.0", "grunt-contrib-connect": "~0.4.1", "grunt-zip": "~0.7.0", "grunt": "~0.4.0" }, "licenses": [ { "type": "MIT", "url": "https://github.com/hakimel/reveal.js/blob/master/LICENSE" } ] }
{ "name": "myPackageName", "description": "this is a very useful description", "homepage": "http://foobar.com" }
{ "version": "2.4.8" }
{ "repository": { "type": "git", "url": "git://github.com/hakimel/reveal.js.git" }, "engines": { "node": "~0.8.0" } }
{ "licenses": [ { "type": "MIT", "url": "https://github.com/hakimel/reveal.js/blob/master/LICENSE" } ] }
{ "dependencies": { "underscore": "~1.5.1", "express": "~2.5.9", "mustache": "~0.7.2", "socket.io": "~0.9.13" } }
{ "main": "lib/index.js" }
{ "scripts": { "test": "mocha --bail test/", "start": "node lib/server.js", "postinstall": "node node_modules/.bin/bower install" } }
A webserver in 11 lines.
var http = require('http'); function requestHandler(req, res) { res.statusCode = 200; res.end('Hello'); } var server = http.createServer(requestHandler); var port = 8000; server.listen(port, function(err) { if(err) console.error('Error when starting server:', err); else console.log('Server started on port %d', port); });
Only one thread for your code: beware of processing time!
function fibo(n) { return n<2 ? 1 : fibo(n-1) + fibo(n-2); } function requestHandler(req, res) { res.end(String(fibo(42))); }
ab -n 10 -c 10 http://localhost:8000/ Requests per second: 0.28 [#/sec] (mean)
var app = require('express')(); app.get('/', function(req, res) { res.send('Hello'); }); app.get('/ping', function(req, res) { res.send('pong'); }); app.get('*', function(req, res) { res.status(404).send('Four Oh Four'); }); app.listen(8000);
var app = express(); app.use(bodyParser.urlEncoded()); app.use(bodyParser.json()); app.use(multipart()); app.use(cookie-parser()); app.use(cookie-session()); app.use(csurf()); app.use(compression());
var app = express(); app.use(bodyParser.urlEncoded()); // decode request's body app.use(bodyParser.json()); // same for json app.use(multipart()); app.use(cookie-parser()); app.use(cookie-session()); app.use(csurf()); app.use(compression());
var app = express(); app.use(bodyParser.urlEncoded()); app.use(bodyParser.json()); app.use(multipart()); // handle files app.use(cookie-parser()); app.use(cookie-session()); app.use(csurf()); app.use(compression());
var app = express(); app.use(bodyParser.urlEncoded()); app.use(bodyParser.json()); app.use(multipart()); app.use(cookie-parser()); // handle cookie app.use(cookie-session()); // sessions app.use(csurf()); app.use(compression());
var app = express(); app.use(bodyParser.urlEncoded()); app.use(bodyParser.json()); app.use(multipart()); app.use(cookie-parser()); app.use(cookie-session()); app.use(csurf()); // CSRF protection app.use(compression());
var app = express(); app.use(bodyParser.urlEncoded()); app.use(bodyParser.json()); app.use(multipart()); app.use(cookie-parser()); app.use(cookie-session()); app.use(csurf()); app.use(compression()); // gzip compression
Interested to write node.js full time ?