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)

PPTX
Introduction to node.js
PDF
Basic API Creation with Node.JS
PPTX
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
PDF
Backbonetutorials
PPTX
Local SQLite Database with Node for beginners
PPTX
Building Web Apps with Express
PPTX
Zero to Hipster with the M.I.K.E. Stack
PDF
GDG-USAR Tech winter break 2024 USAR.pdf
PPT
nodejs tutorial foor free download from academia
KEY
20120306 dublin js
PPTX
Node js crash course session 5
PPTX
Introduction to Node.js
PDF
What is Node.js? (ICON UK)
PDF
Download full ebook of Learning Node Shelley Powers instant download pdf
PDF
Web_Development_with_Node_Express.pdf
PPTX
Express JS
PDF
Introduction to REST API with Node.js
PPTX
Unit IV database intergration with node js
PDF
Backend Basic in nodejs express and mongodb PPT.pdf
PDF
Writing RESTful web services using Node.js
Introduction to node.js
Basic API Creation with Node.JS
MIKE Stack Introduction - MongoDB, io.js, KendoUI, and Express
Backbonetutorials
Local SQLite Database with Node for beginners
Building Web Apps with Express
Zero to Hipster with the M.I.K.E. Stack
GDG-USAR Tech winter break 2024 USAR.pdf
nodejs tutorial foor free download from academia
20120306 dublin js
Node js crash course session 5
Introduction to Node.js
What is Node.js? (ICON UK)
Download full ebook of Learning Node Shelley Powers instant download pdf
Web_Development_with_Node_Express.pdf
Express JS
Introduction to REST API with Node.js
Unit IV database intergration with node js
Backend Basic in nodejs express and mongodb PPT.pdf
Writing RESTful web services using Node.js

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
KodekX | Application Modernization Development
PPTX
A Presentation on Artificial Intelligence
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation theory and applications.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Unlocking AI with Model Context Protocol (MCP)
KodekX | Application Modernization Development
A Presentation on Artificial Intelligence
Encapsulation_ Review paper, used for researhc scholars
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
The AUB Centre for AI in Media Proposal.docx
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I

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