SlideShare a Scribd company logo
Building a real-life
application in Node JS
       Darren Waddell
ME ME ME ME ME

     • MooTools Developer
     • Love JavaScript!
     • C# Developer
     • Love building big apps!
OMG
JAVASCRIPT
SERVER!
‘Real-life applications’

• Websites
• Content Management Systems
• Blog Engines
• (Not games, chat programs, ‘Native HTML5’
  fish demos)
I’LL BUILD
A 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 server




var app = require('express').createServer();

app.get('/', function(req, res){
  res.send('hello world');
});

app.listen(80);
Express
              Setting up View handler



app.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 views



app.get('/foo/bar', function(req, res){
  res.render('foo/bar', {
    locals: {
      foo: ‘bar’
    }
  });
});
Express
             Passing data to views




app.get('/users/:id', function(req, res){
    // req.params contains
    // the querystring values
    var id = req.params.id;
});
Express
              Passing data to views



app.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 DB


select * from ‘foo’

 db.foo.find({});
Mongo DB
insert into ‘foo’
set (‘foobar’)
values (‘whatever’);


db.foo.insert({
   ‘foobar’: ‘whatever’
});
Mongo DB

delete from ‘foo’
where ‘name’ = ‘Darren’



db.foo.remove({
    ‘name’:‘Darren’
});
Mongoose

• Models
• Getters and Setters, Defaults,Validators
• Avoid callback soup (very important!)
Mongoose
                           Simple modelling

var 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 modelling


var 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 modelling




var myUserModel = mongoose.model(‘User’);

myUserModel.username = ‘fakedarren’;
myUserModel.fullname = ‘William Waddell’;

myUserModel.save();
MongooseDefault values




var 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 Setters


var 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 called


User.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
Jade
http://jade-lang.com/


!!! 5
html(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!
!!! 5
html(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!
EJS
https://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!
            @fakedarren

https://github.com/fakedarren/node-cms

More Related Content

PDF
Introduction to Nodejs
PPT
RESTful API In Node Js using Express
PPT
Building your first Node app with Connect & Express
PPTX
introduction to node.js
KEY
Writing robust Node.js applications
PDF
Building servers with Node.js
PDF
Node.js - A Quick Tour
KEY
A million connections and beyond - Node.js at scale
Introduction to Nodejs
RESTful API In Node Js using Express
Building your first Node app with Connect & Express
introduction to node.js
Writing robust Node.js applications
Building servers with Node.js
Node.js - A Quick Tour
A million connections and beyond - Node.js at scale

What's hot (20)

PPTX
Java script at backend nodejs
PDF
Express node js
KEY
node.js: Javascript's in your backend
PPTX
Express js
PDF
Non-blocking I/O, Event loops and node.js
PPT
Node js presentation
PDF
All aboard the NodeJS Express
PDF
Nodejs Explained with Examples
PPTX
Node.js Patterns for Discerning Developers
KEY
Node.js - Best practices
PDF
Node.js
PDF
Node.js and How JavaScript is Changing Server Programming
PPTX
Introduction to node.js GDD
KEY
Introduction to node.js
PPTX
Intro to Node.js (v1)
PDF
PDF
Use Node.js to create a REST API
PDF
Original slides from Ryan Dahl's NodeJs intro talk
KEY
A language for the Internet: Why JavaScript and Node.js is right for Internet...
PDF
NodeJS for Beginner
Java script at backend nodejs
Express node js
node.js: Javascript's in your backend
Express js
Non-blocking I/O, Event loops and node.js
Node js presentation
All aboard the NodeJS Express
Nodejs Explained with Examples
Node.js Patterns for Discerning Developers
Node.js - Best practices
Node.js
Node.js and How JavaScript is Changing Server Programming
Introduction to node.js GDD
Introduction to node.js
Intro to Node.js (v1)
Use Node.js to create a REST API
Original slides from Ryan Dahl's NodeJs intro talk
A language for the Internet: Why JavaScript and Node.js is right for Internet...
NodeJS for Beginner
Ad

Viewers also liked (20)

KEY
Getting Started with MongoDB and Node.js
PDF
Node.js and Ruby
PDF
It is not supposed to fly but it does
PDF
Anatomy of a Modern Node.js Application Architecture
PDF
Node Foundation Membership Overview 20160907
PPTX
Introduction to Node.js
PPTX
S2 b desenvolvimento de sistemas [reparado]
PPTX
Node.js - un poco de informacion.
PDF
Node.js - A Quick Tour II
PDF
Running Node.js in Production using Passenger
PDF
Node in Production at Aviary
PPTX
Node JS (Francisco Cerdas)
PDF
Membangun Website Lowongan Kerja Sederhana dengan NodeJS
PDF
Introducción a Node.js
PDF
Beyond Phoenix
PDF
Webinar: Developing with the modern App Stack: MEAN and MERN (with Angular2 a...
PDF
Nodifying the Enterprise - Prince Soni, TO THE NEW
PPTX
Connecting NodeJS & MongoDB
PPTX
Node JS Express : Steps to Create Restful Web App
Getting Started with MongoDB and Node.js
Node.js and Ruby
It is not supposed to fly but it does
Anatomy of a Modern Node.js Application Architecture
Node Foundation Membership Overview 20160907
Introduction to Node.js
S2 b desenvolvimento de sistemas [reparado]
Node.js - un poco de informacion.
Node.js - A Quick Tour II
Running Node.js in Production using Passenger
Node in Production at Aviary
Node JS (Francisco Cerdas)
Membangun Website Lowongan Kerja Sederhana dengan NodeJS
Introducción a Node.js
Beyond Phoenix
Webinar: Developing with the modern App Stack: MEAN and MERN (with Angular2 a...
Nodifying the Enterprise - Prince Soni, TO THE NEW
Connecting NodeJS & MongoDB
Node JS Express : Steps to Create Restful Web App
Ad

Similar to Building a real life application in node js (20)

PDF
Javascript MVC & Backbone Tips & Tricks
PPTX
Introduction to node.js
KEY
The go-start webframework (GTUG Vienna 27.03.2012)
PDF
Build web application by express
PDF
Mojolicious, real-time web framework
PDF
WordPress as the Backbone(.js)
PPTX
Beyond DOMReady: Ultra High-Performance Javascript
PPTX
Javascript first-class citizenery
PDF
SproutCore and the Future of Web Apps
PDF
Styling components with JavaScript
KEY
[Coscup 2012] JavascriptMVC
KEY
Week 4 - jQuery + Ajax
PDF
Dependency Management with RequireJS
PDF
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
PPTX
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
PDF
WebNet Conference 2012 - Designing complex applications using html5 and knock...
KEY
#NewMeetup Performance
PDF
Practical HTML5: Using It Today
PDF
An Introduction to Tornado
PDF
PHP and Rich Internet Applications
Javascript MVC & Backbone Tips & Tricks
Introduction to node.js
The go-start webframework (GTUG Vienna 27.03.2012)
Build web application by express
Mojolicious, real-time web framework
WordPress as the Backbone(.js)
Beyond DOMReady: Ultra High-Performance Javascript
Javascript first-class citizenery
SproutCore and the Future of Web Apps
Styling components with JavaScript
[Coscup 2012] JavascriptMVC
Week 4 - jQuery + Ajax
Dependency Management with RequireJS
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
WebNet Conference 2012 - Designing complex applications using html5 and knock...
#NewMeetup Performance
Practical HTML5: Using It Today
An Introduction to Tornado
PHP and Rich Internet Applications

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Approach and Philosophy of On baking technology
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
Spectroscopy.pptx food analysis technology
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25-Week II
MYSQL Presentation for SQL database connectivity
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Spectral efficient network and resource selection model in 5G networks
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Approach and Philosophy of On baking technology
SOPHOS-XG Firewall Administrator PPT.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Group 1 Presentation -Planning and Decision Making .pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Advanced methodologies resolving dimensionality complications for autism neur...
Assigned Numbers - 2025 - Bluetooth® Document
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
A comparative analysis of optical character recognition models for extracting...
Spectroscopy.pptx food analysis technology

Building a real life application in node js

  • 1. Building a real-life application in Node JS Darren Waddell
  • 2. ME ME ME ME ME • MooTools Developer • Love JavaScript! • C# Developer • Love building big apps!
  • 4. ‘Real-life applications’ • Websites • Content Management Systems • Blog Engines • (Not games, chat programs, ‘Native HTML5’ fish demos)
  • 6. What did I need to start • OSX! (or linux) • Install Node and NPM • A DB (MongoDB)
  • 7. Confusing things! • Non-blocking, event driven, asynchronous • NoSQL • Modules and NPM • No Firebug!
  • 8. Things I need for my app • Routing • Database • Templating
  • 9. Express Setting up server var app = require('express').createServer(); app.get('/', function(req, res){ res.send('hello world'); }); app.listen(80);
  • 10. Express Setting up View handler app.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. 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. Express Passing data to views app.get('/foo/bar', function(req, res){ res.render('foo/bar', { locals: { foo: ‘bar’ } }); });
  • 13. Express Passing data to views app.get('/users/:id', function(req, res){ // req.params contains // the querystring values var id = req.params.id; });
  • 14. Express Passing data to views app.use(express.bodyParser()); app.post('/users/:id', function(req, res){ // req.body contains the postback var username = req.body.name; });
  • 15. Express ‘Master Pages’ app.set('view options', { layout: 'shared/layout' }); app.get('/foo/bar', function(req, res){ res.render('foo/bar'); });
  • 16. Things I need for my app • Routing • Database • Templating
  • 17. Mongo DB • No tables! ‘Collections’ • No Rows! ‘Documents’ • No columns! ‘Fields’ • JSON format! • No SQL joins! • Flexible table collection structure
  • 18. Mongo DB select * from ‘foo’ db.foo.find({});
  • 19. Mongo DB insert into ‘foo’ set (‘foobar’) values (‘whatever’); db.foo.insert({ ‘foobar’: ‘whatever’ });
  • 20. Mongo DB delete from ‘foo’ where ‘name’ = ‘Darren’ db.foo.remove({ ‘name’:‘Darren’ });
  • 21. Mongoose • Models • Getters and Setters, Defaults,Validators • Avoid callback soup (very important!)
  • 22. Mongoose Simple modelling var 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. Mongoose Simple modelling var 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. Mongoose Simple modelling var myUserModel = mongoose.model(‘User’); myUserModel.username = ‘fakedarren’; myUserModel.fullname = ‘William Waddell’; myUserModel.save();
  • 25. MongooseDefault values var 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. Mongoose Getters and Setters var 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. Mongoose Middleware / promises / whatever it’s called User.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. Things I need for my app • Routing • Database • Templating
  • 29. View Engines HAML Jade EJS CoffeeKup jQuery Templates
  • 30. View Engines HAML Jade EJS CoffeeKup jQuery Templates
  • 31. Jade http://jade-lang.com/ !!! 5 html(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. !!! 5 html(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. EJS https://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. <!DOCTYPE html> <html> <head> <title>Awesome</title> </head> <body> <% if (names.length) { %> <ul>     <% names.forEach(function(name){ %>       <li><%= name %></li>     <% }) %>   </ul> <% } %> </body> </html>
  • 35. Things I need for my app • Routing • Database • Templating
  • 36. Things I need for my app • Routing • Database • Templating • And a million other things....
  • 37. Thanks! @fakedarren https://github.com/fakedarren/node-cms

Editor's Notes