• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Building a real life application in node js
 

Building a real life application in node js

on

  • 23,440 views

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

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

Statistics

Views

Total Views
23,440
Views on SlideShare
19,303
Embed Views
4,137

Actions

Likes
20
Downloads
225
Comments
1

18 Embeds 4,137

http://www.maestrosdelweb.com 3974
http://integradorweb.com 84
http://feeds.feedburner.com 24
http://mdw.diegoescares.com 16
http://paper.li 12
http://flavors.me 8
http://inteligenciaartificialomb.com 4
http://translate.googleusercontent.com 2
http://webcache.googleusercontent.com 2
http://foros.collected.info 2
http://twitter.com 2
https://www.google.com 1
http://www.red-geek.com.ar 1
http://optimizaciondemotoresdebusqueda.info 1
http://127.0.0.1 1
http://www.newsblur.com 1
https://twitter.com 1
http://alok.softvision.in 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

11 of 1 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • great! wow!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Building a real life application in node js Building a real life application in node js Presentation Transcript

  • Building a real-lifeapplication in Node JS Darren Waddell
  • ME ME ME ME ME • MooTools Developer • Love JavaScript! • C# Developer • Love building big apps!
  • OMGJAVASCRIPTSERVER!
  • ‘Real-life applications’• Websites• Content Management Systems• Blog Engines• (Not games, chat programs, ‘Native HTML5’ fish demos)
  • I’LL BUILDA CMS!
  • What did I need to start • OSX! (or linux) • Install Node and NPM • A DB (MongoDB)
  • Confusing things!• Non-blocking, event driven, asynchronous• NoSQL• Modules and NPM• No Firebug!
  • Things I need for my app• Routing• Database• Templating
  • Express Setting up servervar app = require(express).createServer();app.get(/, function(req, res){ res.send(hello world);});app.listen(80);
  • 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);
  • 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
  • Express Passing data to viewsapp.get(/foo/bar, function(req, res){ res.render(foo/bar, { locals: { foo: ‘bar’ } });});
  • Express Passing data to viewsapp.get(/users/:id, function(req, res){ // req.params contains // the querystring values var id = req.params.id;});
  • 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;});
  • Express ‘Master Pages’app.set(view options, { layout: shared/layout});app.get(/foo/bar, function(req, res){ res.render(foo/bar);});
  • Things I need for my app• Routing• Database• Templating
  • Mongo DB• No tables! ‘Collections’• No Rows! ‘Documents’• No columns! ‘Fields’• JSON format!• No SQL joins!• Flexible table collection structure
  • Mongo DBselect * from ‘foo’ db.foo.find({});
  • Mongo DBinsert into ‘foo’set (‘foobar’)values (‘whatever’);db.foo.insert({ ‘foobar’: ‘whatever’});
  • Mongo DBdelete from ‘foo’where ‘name’ = ‘Darren’db.foo.remove({ ‘name’:‘Darren’});
  • Mongoose• Models• Getters and Setters, Defaults,Validators• Avoid callback soup (very important!)
  • 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 });});
  • 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();
  • Mongoose Simple modellingvar myUserModel = mongoose.model(‘User’);myUserModel.username = ‘fakedarren’;myUserModel.fullname = ‘William Waddell’;myUserModel.save();
  • 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’ }});
  • 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; }});
  • 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();
  • Things I need for my app• Routing• Database• Templating
  • View Engines HAML Jade EJS CoffeeKup jQuery Templates
  • View Engines HAML Jade EJS CoffeeKup jQuery Templates
  • 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!
  • !!! 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!
  • 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>
  • <!DOCTYPE html><html><head><title>Awesome</title></head><body><% if (names.length) { %><ul>    <% names.forEach(function(name){ %>      <li><%= name %></li>    <% }) %>  </ul><% } %></body></html>
  • Things I need for my app• Routing• Database• Templating
  • Things I need for my app• Routing• Database• Templating• And a million other things....
  • Thanks! @fakedarrenhttps://github.com/fakedarren/node-cms