Successfully reported this slideshow.

Building a real life application in node js

28,661 views

Published on

A talk I gave at London Ajax about building a Node app

Published in: Technology

Building a real life application in node js

  1. 1. Building a real-lifeapplication in Node JS Darren Waddell
  2. 2. ME ME ME ME ME • MooTools Developer • Love JavaScript! • C# Developer • Love building big apps!
  3. 3. OMGJAVASCRIPTSERVER!
  4. 4. ‘Real-life applications’• Websites• Content Management Systems• Blog Engines• (Not games, chat programs, ‘Native HTML5’ fish demos)
  5. 5. I’LL BUILDA CMS!
  6. 6. What did I need to start • OSX! (or linux) • Install Node and NPM • A DB (MongoDB)
  7. 7. Confusing things!• Non-blocking, event driven, asynchronous• NoSQL• Modules and NPM• No Firebug!
  8. 8. Things I need for my app• Routing• Database• Templating
  9. 9. Express Setting up servervar app = require(express).createServer();app.get(/, function(req, res){ res.send(hello world);});app.listen(80);
  10. 10. Express Setting up View handlerapp.set(view engine, jade);app.set(views, __dirname + /views);app.get(/foo/bar, function(req, res){ res.render(foo/bar); // calls /views/foo/bar.jade});app.listen(80);
  11. 11. Express Setting up ‘static assets’var app = require(express).createServer();app.use( express.static(__dirname + /public));<script type=”foo.js”></script>// is at http://foo.com/foo.js// NOT http://foo.com/public/foo.js
  12. 12. Express Passing data to viewsapp.get(/foo/bar, function(req, res){ res.render(foo/bar, { locals: { foo: ‘bar’ } });});
  13. 13. Express Passing data to viewsapp.get(/users/:id, function(req, res){ // req.params contains // the querystring values var id = req.params.id;});
  14. 14. Express Passing data to viewsapp.use(express.bodyParser());app.post(/users/:id, function(req, res){ // req.body contains the postback var username = req.body.name;});
  15. 15. Express ‘Master Pages’app.set(view options, { layout: shared/layout});app.get(/foo/bar, function(req, res){ res.render(foo/bar);});
  16. 16. Things I need for my app• Routing• Database• Templating
  17. 17. Mongo DB• No tables! ‘Collections’• No Rows! ‘Documents’• No columns! ‘Fields’• JSON format!• No SQL joins!• Flexible table collection structure
  18. 18. Mongo DBselect * from ‘foo’ db.foo.find({});
  19. 19. Mongo DBinsert into ‘foo’set (‘foobar’)values (‘whatever’);db.foo.insert({ ‘foobar’: ‘whatever’});
  20. 20. Mongo DBdelete from ‘foo’where ‘name’ = ‘Darren’db.foo.remove({ ‘name’:‘Darren’});
  21. 21. Mongoose• Models• Getters and Setters, Defaults,Validators• Avoid callback soup (very important!)
  22. 22. Mongoose Simple modellingvar mongoose = require(‘mongoose’);mongoose.connect(‘mongodb://localhost/myDB’);var User = new mongoose.Schema({ username: String, fullname: String, password: String});var myUserModel = mongoose.model(‘User’);myUserModel.find({}, function(err, docs){ docs.forEach(function(user){ // do stuff });});
  23. 23. Mongoose Simple modellingvar mongoose = require(‘mongoose’);mongoose.connect(‘mongodb://localhost/myDB’);var User = new mongoose.Schema({ username: String, fullname: String, password: String});var myUserModel = mongoose.model(‘User’);myUserModel.username = ‘fakedarren’;myUserModel.fullname = ‘William Waddell’;myUserModel.save();
  24. 24. Mongoose Simple modellingvar myUserModel = mongoose.model(‘User’);myUserModel.username = ‘fakedarren’;myUserModel.fullname = ‘William Waddell’;myUserModel.save();
  25. 25. MongooseDefault valuesvar mongoose = require(‘mongoose’);mongoose.connect(‘mongodb://localhost/myDB’);var User = new mongoose.Schema({ username: String, fullname: String, password: String, role: { type: String, default: ‘Admin’ }});
  26. 26. Mongoose Getters and Settersvar mongoose = require(‘mongoose’);mongoose.connect(‘mongodb://localhost/myDB’);var User = new mongoose.Schema({ username: String, fullname: String, password: String});User.path(‘password’).set(function(value){ if (password.length < 8){ return new Error(‘Password must be more than 8 characters’); } else { return value; }});
  27. 27. Mongoose Middleware / promises / whatever it’s calledUser.pre(‘save’, function(next){ // do something next();});User.pre(‘save’, function(next){ // do something else next();});var myUserModel = mongoose.model(‘User’);myUserModel.username = ‘fakedarren’;myUserModel.fullname = ‘William Waddell’;myUserModel.save();
  28. 28. Things I need for my app• Routing• Database• Templating
  29. 29. View Engines HAML Jade EJS CoffeeKup jQuery Templates
  30. 30. View Engines HAML Jade EJS CoffeeKup jQuery Templates
  31. 31. Jadehttp://jade-lang.com/!!! 5html(lang="en") head title= pageTitle script(type=text/javascript) if (foo) { bar() } body h1 Jade - node template engine #container - if (youAreUsingJade) p You are amazing - else p Get on it!
  32. 32. !!! 5html(lang="en") head title= pageTitle script(type=text/javascript) if (foo) { bar() } body h1 Jade - node template engine #container - if (youAreUsingJade) p You are amazing - else p Get on it!
  33. 33. EJShttps://github.com/visionmedia/ejs <!DOCTYPE html> <html> <head> <title>Awesome</title> </head> <body> <% if (names.length) { %> <ul>     <% names.forEach(function(name){ %>       <li><%= name %></li>     <% }) %>   </ul> <% } %> </body> </html>
  34. 34. <!DOCTYPE html><html><head><title>Awesome</title></head><body><% if (names.length) { %><ul>    <% names.forEach(function(name){ %>      <li><%= name %></li>    <% }) %>  </ul><% } %></body></html>
  35. 35. Things I need for my app• Routing• Database• Templating
  36. 36. Things I need for my app• Routing• Database• Templating• And a million other things....
  37. 37. Thanks! @fakedarrenhttps://github.com/fakedarren/node-cms

×