0
Agenda
ž  Introduction
ž  Package Manager
ž  Web Framework
ž  Testing
ž  Callback Hell
ž  Obfuscator
ž  Build Tool
Introduction
What is Node.js
ž  Server Side JavaScript
ž  Based on Chrome’s V8 Engines
ž  Non Blocking I/O
ž  15,000+ Modules
ž  A...
What is Node.js
Who's using Node.js
Who's using Node.js
Why Node.js
ž  Apache v.s. Nginx
Why Node.js
ž  Apache v.s. Nginx
Blocking I/L
$query = 'SELECT * FROM users WHERE id = ?';
$users = query($query, array($id));
print_r($users);
$query = '...
Non-Blocking I/J
var query = 'SELECT * FROM users WHERE id = ?';
db.query(query, [userId], function (err, results) {
cons...
Why Node.js
Why Node.js
Installing Node.js
Mac OS X
1.  Go to http://nodejs.org and click install
2.  Install the downloaded package
Windows
1.  G...
Hello World
Create hello-world.js
console.log(‘Hello World’);
On the command line run
node hello-world.js
You should see
H...
Basic HTTP Server
var http = require('http');
var server = http.createServer(function (req, res) {
res.writeHead(200);
res...
Foreign Function Interface
ž  node-ffi
—  a Node.js addon for loading and calling
dynamic libraries using pure JavaScrip...
Package Manager
NPM
ž  Simple CLI tools
ž  Register registry.npmjs.org
ž  Web site npmjs.org
ž  Included in node.js since 0.6
ž  Git ...
NPM Usage
ž  npm search express
ž  npm info express
ž  npm install -g express
ž  npm update
ž  npm publish
NPM Usage
ž  npm install ./package.tgz
ž  npm install sax --save
ž  npm install node-tap --save-dev
ž  npm install dtr...
Module Metas
ž  package.json
{
"name": "my-website",
"version": "0.0.1",
"dependencies": {
"express": "2.4.7",
"jade": ">...
Transitive dependencies
Virtual Environment
ž  nvm (starred 3884 on github)
—  Simple bash script to manage multiple active
node.js versions
$ n...
Virtual Environment
ž  nave (starred 645 on github)
—  Virtual Environments for Node
—  run on various node.js builds
$...
Web Framework
Express
ž  Express is a minimal and flexible node.js
web application framework, providing a
robust set of features for bu...
Express
ž  Generate an app
$ npm install -g express-generator
$ express --css stylus myapp
create : myapp
create : myapp/...
Express Middleware
ž  Middleware is any number of functions
that are invoked by the Express.js
routing layer before your ...
Express Middleware
var app = express();
app.use(function(req, res, next) {
console.log('%s %s', req.method, req.url);
next...
Express Middleware
ž  body-parser
—  body
—  co-body
—  raw-body
ž  compression
ž  connect-timeout
ž  cookie-parser...
Express I18N
app.js
var i18n = require('i18n');
app.use(i18n.init);
app.use(function (req, res, next) {
res.locals.__ = fu...
Express Authentication
ž  Passport
—  authentication middleware for Node.js.
app.post('/login',
passport.authenticate('l...
Template Engine
ž  Jade
ž  EJS
ž  Hogan.js
Jade
doctype html
html(lang="en")
head
title= pageTitle
script(type='text/javascript').
if (foo) {
bar(1 + 5)
}
body
h1 Ja...
Jade
var fn = jade.compile(jadeTemplate);
var htmlOutput = fn({
data = {'title': 'Cleaning Supplies', 'supplies': ['mop', ...
EJS
data = {'title': 'Cleaning Supplies', 'supplies': ['mop', 'broom', 'duster']}
var html = new EJS({url: 'cleaning.ejs'}...
Hogan.js
data = {'title': 'Cleaning Supplies', 'supplies': ['mop', 'broom', 'duster']}
var template = Hogan.compile(text);...
Redis Driver
$ npm install redis hiredis
var redis = require("redis"),
client = redis.createClient();
// if you'd like to ...
Redis Sentinel Driver
$ npm install redis hiredis redis-sentinel
var sentinel = require('redis-sentinel');
var endpoints =...
Mongo Driver
$ npm install mongodb
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://192.16...
Mongo Driver
ž  Mongoose
—  Mongoose provides a straight-forward, schema-based
solution to modeling your application dat...
Testing
Debugging
ž  Node-inspector
—  Node Inspector is a debugger interface for Node.js
applications that uses the Blink Devel...
Testing Framework
ž  Mocha
—  Mocha is a feature-rich JavaScript test
framework running on node.js and the
browser, maki...
Mocha
var assert = require("assert");
describe('Test', function() {
var foo;
beforeEach(function() {
console.log('setup');...
Nodeunit
module.exports = {
setUp: function (callback) {
console.log('setup');
this.foo = 'bar';
callback();
},
tearDown: ...
Callback Hell
Callback Hell
doAsync1(function() {
doAsync2(function() {
doAsync3(function() {
doAsync4(function() {
});
});
});
});
Callback Hell
ž  An example from http://callbackhell.com/
fs.readdir(source, function(err, files) {
if (err) {
console.lo...
Avoiding Callback Hell
ž  Keep your code shallow
ž  Break up your code into small
chunks(modularize)
ž  Use a sequentia...
async approach
ž  Async is a utility module which provides
straight-forward, powerful functions for
working with asynchro...
async approach
var async = require('async');
var db = require(’db');
function getUser (callback) {
var query = 'SELECT * F...
promises approach
ž  A tool for making and composing
asynchronous promises in JavaScript
$ npm install q
promises approach
var q = require('q');
var fs = require('fs');
var request = require('request');
var readFile = q.denodei...
generator approach
ž  Generators are lightweight co-routines
for JavaScript.
ž  Available in Node >=0.11.2 or above
generator approach
var co = require('co');
var thunkify = require('thunkify');
var request = require('request');
var get =...
Obfuscator
Obfuscator
ž  YUI Compressor
ž  Google Closure Compiler
ž  minifyjs
ž  uglify-js
uglifyjs app.js -m -o app-min.js
ž  ...
Obfuscator
ž  Write your extension
—  Implement encrypt/decrypt functions
require.extensions[".jse"] = function (m) {
m....
Precompilation
ž  Nexe
—  Nexe is a command-line utility that compiles
your Node.js application into a single
executable...
Build Tool
Make
ž  GNU Make is a tool which controls the
generation of executables and other
non-source files of a program from the
...
Grunt
ž  JavaScript Task Runner
—  minification
—  compilation
—  unit testing
—  …….
ž  Thousands of plugins
Questions?
Node js introduction
Upcoming SlideShare
Loading in...5
×

Node js introduction

1,914

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,914
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Node js introduction"

  1. 1. Agenda ž  Introduction ž  Package Manager ž  Web Framework ž  Testing ž  Callback Hell ž  Obfuscator ž  Build Tool
  2. 2. Introduction
  3. 3. What is Node.js ž  Server Side JavaScript ž  Based on Chrome’s V8 Engines ž  Non Blocking I/O ž  15,000+ Modules ž  Active Community (IRC, Mailing Lists, Twitter, Github) ž  Mac, Linux and Windows (all first class citizens) ž  One Language for Frontend and Backend ž  Node is a platform for JavaScript applications which is not to be confused with a framework.
  4. 4. What is Node.js
  5. 5. Who's using Node.js
  6. 6. Who's using Node.js
  7. 7. Why Node.js ž  Apache v.s. Nginx
  8. 8. Why Node.js ž  Apache v.s. Nginx
  9. 9. Blocking I/L $query = 'SELECT * FROM users WHERE id = ?'; $users = query($query, array($id)); print_r($users); $query = 'SELECT * FROM activities ORDER BY timestamp LIMIT 50'; $activities = query($query); print_r($activities); $query = 'SELECT count(points) as total, user_id FROM activities LIMIT 50'; $leader_board = query($query);
  10. 10. Non-Blocking I/J var query = 'SELECT * FROM users WHERE id = ?'; db.query(query, [userId], function (err, results) { console.log(results); }); var query = 'SELECT * FROM activities ORDER BY timestamp LIMIT 50'; db.query(query, function (err, results) { console.log(results); }); var query = 'SELECT count(points) as total, user_id FROM activities LIMIT 50'; db.query(query, function (err, results) { console.log(results); });
  11. 11. Why Node.js
  12. 12. Why Node.js
  13. 13. Installing Node.js Mac OS X 1.  Go to http://nodejs.org and click install 2.  Install the downloaded package Windows 1.  Go to http://nodejs.org and click install 2.  Install the downloaded package Linux (and *nix variants) 1.  Go to http://nodejs.org and click install 2.  Decompress source and… ./configure … make … make install ( for Ubuntu use Chris Lea’s PPA – ppa:chris-lea/node.js )
  14. 14. Hello World Create hello-world.js console.log(‘Hello World’); On the command line run node hello-world.js You should see Hello World
  15. 15. Basic HTTP Server var http = require('http'); var server = http.createServer(function (req, res) { res.writeHead(200); res.end('Hello World'); }); server.listen(4000);
  16. 16. Foreign Function Interface ž  node-ffi —  a Node.js addon for loading and calling dynamic libraries using pure JavaScript. It can be used to create bindings to native libraries without writing any C++ code.
  17. 17. Package Manager
  18. 18. NPM ž  Simple CLI tools ž  Register registry.npmjs.org ž  Web site npmjs.org ž  Included in node.js since 0.6 ž  Git friendly
  19. 19. NPM Usage ž  npm search express ž  npm info express ž  npm install -g express ž  npm update ž  npm publish
  20. 20. NPM Usage ž  npm install ./package.tgz ž  npm install sax --save ž  npm install node-tap --save-dev ž  npm install dtrace-provider --save-optional ž  npm install https://github.com/indexzero/forever/ tarball/v0.5.6
  21. 21. Module Metas ž  package.json { "name": "my-website", "version": "0.0.1", "dependencies": { "express": "2.4.7", "jade": ">= 0.0.1", "oauth": ">= 0.9.5" } }
  22. 22. Transitive dependencies
  23. 23. Virtual Environment ž  nvm (starred 3884 on github) —  Simple bash script to manage multiple active node.js versions $ nvm install 0.10.29 $ nvm ls v0.10.29 current: v0.10.29 $ nvm use 0.10.29 Now using node v0.10.29 $ node --version v0.10.29
  24. 24. Virtual Environment ž  nave (starred 645 on github) —  Virtual Environments for Node —  run on various node.js builds $ nave install latest $ nave ls src: 0.10.28 0.11.13 installed: 0.10.28 0.11.13 $ nave use 0.11.13 Already installed: 0.11.13 using 0.11.13 $ node --version v0.11.13
  25. 25. Web Framework
  26. 26. Express ž  Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.
  27. 27. Express ž  Generate an app $ npm install -g express-generator $ express --css stylus myapp create : myapp create : myapp/package.json create : myapp/app.js create : myapp/public create : myapp/public/javascripts create : myapp/public/images create : myapp/routes create : myapp/routes/index.js create : myapp/routes/users.js create : myapp/public/stylesheets create : myapp/public/stylesheets/style.styl create : myapp/views create : myapp/views/index.jade create : myapp/views/layout.jade create : myapp/views/error.jade create : myapp/bin create : myapp/bin/www install dependencies: $ cd myapp && npm install run the app: $ DEBUG=myapp ./bin/www
  28. 28. Express Middleware ž  Middleware is any number of functions that are invoked by the Express.js routing layer before your final request handler is
  29. 29. Express Middleware var app = express(); app.use(function(req, res, next) { console.log('%s %s', req.method, req.url); next(); }); app.get('/', function(req, res, next) { res.send('Hello World!'); }); app.get('/help', function(req, res, next) { res.send('Nope.. nothing to see here'); });
  30. 30. Express Middleware ž  body-parser —  body —  co-body —  raw-body ž  compression ž  connect-timeout ž  cookie-parser ž  cookie-session ž  csurf ž  errorhandler ž  express-session ž  method-override ž  morgan ž  response-time ž  serve-favicon ž  serve-index ž  serve-static ž  vhost
  31. 31. Express I18N app.js var i18n = require('i18n'); app.use(i18n.init); app.use(function (req, res, next) { res.locals.__ = function () { return function (text, render) { return i18n.__.apply(req, arguments); }; }; next(); }); en.js { "Home": "Home", "About Us": "About Us", "Program": "Program", "Venue": "Venue", "Sponsors": "Sponsors" } menu.hjs <li>{{#__}}Home{{/__}}</li> <li>{{#__}}About Us{{/__}}</li> <li>{{#__}}Program{{/__}}</li> <li>{{#__}}Venue{{/__}}</li> <li>{{#__}}Sponsors{{/__}}</li> menu.html <li>Home</li> <li>About Us</li> <li>Program</li> <li>Venue</li> <li>Sponsors</li>
  32. 32. Express Authentication ž  Passport —  authentication middleware for Node.js. app.post('/login', passport.authenticate('local'), function(req, res) { res.redirect('/users/' + req.user.username); }); app.use(function(req, res, next) { if (!req.session.userid) { return res.redirect(url); } next(); });
  33. 33. Template Engine ž  Jade ž  EJS ž  Hogan.js
  34. 34. Jade doctype html html(lang="en") head title= pageTitle script(type='text/javascript'). if (foo) { bar(1 + 5) } body h1 Jade - node template engine #container.col if youAreUsingJade p You are amazing else p Get on it! p. Jade is a terse and simple templating language with a strong focus on performance and powerful features. <!DOCTYPE html> <html lang="en"> <head> <title>Jade</title> <script type="text/javascript"> if (foo) { bar(1 + 5) } </script> </head> <body> <h1>Jade - node template engine</h1> <div id="container" class="col"> <p>You are amazing</p> <p> Jade is a terse and simple templating language with a strong focus on performance and powerful features. </p> </div> </body> </html>
  35. 35. Jade var fn = jade.compile(jadeTemplate); var htmlOutput = fn({ data = {'title': 'Cleaning Supplies', 'supplies': ['mop', 'broom', 'duster']} }); h1= title ul each supply in supplies li= supply <h1>Cleaning supplies</h1> <ul> <li>mop</li> <li>broom</li> <li>duster</li> </ul>
  36. 36. EJS data = {'title': 'Cleaning Supplies', 'supplies': ['mop', 'broom', 'duster']} var html = new EJS({url: 'cleaning.ejs'}).render(data); <h1><%= title %></h1> <ul> <% for(var i=0; i<supplies.length; i++) {%> <li><%= supplies[i] %></li> <% } %> </ul> <h1>Cleaning supplies</h1> <ul> <li>mop</li> <li>broom</li> <li>duster</li> </ul>
  37. 37. Hogan.js data = {'title': 'Cleaning Supplies', 'supplies': ['mop', 'broom', 'duster']} var template = Hogan.compile(text); var output = template.render(data); <h1>{{ title }}</h1> <ul> {{#supplies}} <li>{{.}}</li> {{/supplies}} </ul> <h1>Cleaning supplies</h1> <ul> <li>mop</li> <li>broom</li> <li>duster</li> </ul>
  38. 38. Redis Driver $ npm install redis hiredis var redis = require("redis"), client = redis.createClient(); // if you'd like to select database 3, instead of 0 (default), call // client.select(3, function() { /* ... */ }); client.on("error", function (err) { console.log("Error " + err); }); client.set("string key", "string val", redis.print); client.hset("hash key", "hashtest 1", "some value", redis.print); client.hset(["hash key", "hashtest 2", "some other value"], redis.print); client.hkeys("hash key", function (err, replies) { console.log(replies.length + " replies:"); replies.forEach(function (reply, i) { console.log(" " + i + ": " + reply); }); client.quit(); });
  39. 39. Redis Sentinel Driver $ npm install redis hiredis redis-sentinel var sentinel = require('redis-sentinel'); var endpoints = [ {host: '127.0.0.1', port: 26379}, {host: '127.0.0.1', port: 26380} ]; var opts = {}; var masterName = 'mymaster'; var redisClient = sentinel.createClient(endpoints, masterName, opts);
  40. 40. Mongo Driver $ npm install mongodb var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://192.168.68.234:27017/qcloud', function(err, db) { if(err) throw err; var collection = db.collection('device'); collection.find({'device_name':'alexnas'}).toArray(function(err, results) { console.dir(results); db.close(); }); });
  41. 41. Mongo Driver ž  Mongoose —  Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more, out of the box. var mongoose = require('mongoose'); mongoose.connect('mongodb://192.168.68.234:27017/alex_test'); var Cat = mongoose.model('Cat', { name: String }); var kitty = new Cat({ name: 'Zildjian' }); kitty.save(function (err) { console.log('meow'); });
  42. 42. Testing
  43. 43. Debugging ž  Node-inspector —  Node Inspector is a debugger interface for Node.js applications that uses the Blink Developer Tools (formerly WebKit Web Inspector). —  Node Inspector works in Chrome and Opera only. $ npm install -g node-inspector $ node-debug app.js or $ node-inspector & $ node --debug app.js
  44. 44. Testing Framework ž  Mocha —  Mocha is a feature-rich JavaScript test framework running on node.js and the browser, making asynchronous testing simple and fun. ž  Nodeunit —  Simple syntax, powerful tools. Nodeunit provides easy async unit testing for node.js and the browser.
  45. 45. Mocha var assert = require("assert"); describe('Test', function() { var foo; beforeEach(function() { console.log('setup'); foo = 'bar'; }); afterEach(function() { console.log('teardown'); }); it('#test1', function() { console.log('run test1'); assert.equal(foo, 'bar'); }); });
  46. 46. Nodeunit module.exports = { setUp: function (callback) { console.log('setup'); this.foo = 'bar'; callback(); }, tearDown: function (callback) { console.log('teardown'); callback(); }, test1: function (test) { console.log('run test1'); test.equals(this.foo, 'bar'); test.done(); } };
  47. 47. Callback Hell
  48. 48. Callback Hell doAsync1(function() { doAsync2(function() { doAsync3(function() { doAsync4(function() { }); }); }); });
  49. 49. Callback Hell ž  An example from http://callbackhell.com/ fs.readdir(source, function(err, files) { if (err) { console.log('Error finding files: ' + err) } else { files.forEach(function(filename, fileIndex) { console.log(filename) gm(source + filename).size(function(err, values) { if (err) { console.log('Error identifying file size: ' + err) } else { console.log(filename + ' : ' + values) aspect = (values.width / values.height) widths.forEach( function(width, widthIndex) { height = Math.round(width / aspect) console.log('resizing ' + filename + 'to ' + height + 'x' + height) this.resize(width, height).write(destination + 'w' + width + '_' + filename, function(err) { if (err) console.log('Error writing file: ' + err) }) }.bind(this)) } }) }) } })
  50. 50. Avoiding Callback Hell ž  Keep your code shallow ž  Break up your code into small chunks(modularize) ž  Use a sequential library like async
  51. 51. async approach ž  Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. $ npm install async
  52. 52. async approach var async = require('async'); var db = require(’db'); function getUser (callback) { var query = 'SELECT * FROM users WHERE id = ?'; db.query(query, [userId], callback); } function getActivities (callback) { var query = 'SELECT * FROM activities ORDER BY timestamp LIMIT 50'; db.query(query, callback); } function getLeaderBoard (callback) { var query = 'SELECT count(points) as total, user_id FROM activities LIMIT 50'; db.query(query, callback); } var tasks = [getUser, getActivities, getLeaderBoard]; async.parallel(tasks, function (err, results) { var user = results[0][0]; var activities = results[1]; var leaderBoard = results[2]; });
  53. 53. promises approach ž  A tool for making and composing asynchronous promises in JavaScript $ npm install q
  54. 54. promises approach var q = require('q'); var fs = require('fs'); var request = require('request'); var readFile = q.denodeify(fs.readFile); var q_request = q.denodeify(request); readFile('url.txt', 'utf-8') .then(function(text) { return q_request(text); }) .then(function(result) { console.log(result[0].statusCode); }) .fail(function(err) { console.log('an error occurs : ' + err); });
  55. 55. generator approach ž  Generators are lightweight co-routines for JavaScript. ž  Available in Node >=0.11.2 or above
  56. 56. generator approach var co = require('co'); var thunkify = require('thunkify'); var request = require('request'); var get = thunkify(request.get); co(function *(){ var a = yield get('http://google.com'); var b = yield get('http://yahoo.com'); var c = yield get('http://cloudup.com'); console.log(a[0].statusCode); console.log(b[0].statusCode); console.log(c[0].statusCode); })() co(function *(){ var a = get('http://google.com'); var b = get('http://yahoo.com'); var c = get('http://cloudup.com'); var res = yield [a, b, c]; console.log(res); })()
  57. 57. Obfuscator
  58. 58. Obfuscator ž  YUI Compressor ž  Google Closure Compiler ž  minifyjs ž  uglify-js uglifyjs app.js -m -o app-min.js ž  node-obfuscator obfuscator --entry app.js app.js routes/index.js routes/user.js
  59. 59. Obfuscator ž  Write your extension —  Implement encrypt/decrypt functions require.extensions[".jse"] = function (m) { m.exports = MyNativeExtension.decrypt(fs.readFileSync(m.filename)); }; require("YourCode.jse");
  60. 60. Precompilation ž  Nexe —  Nexe is a command-line utility that compiles your Node.js application into a single executable file.
  61. 61. Build Tool
  62. 62. Make ž  GNU Make is a tool which controls the generation of executables and other non-source files of a program from the program's source files.
  63. 63. Grunt ž  JavaScript Task Runner —  minification —  compilation —  unit testing —  ……. ž  Thousands of plugins
  64. 64. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×