NodeSchool Bristol #1

21st February 2015

Node Libraries

Express

Minimalist web framework: routing, templating & middleware

var express = require('express'),
    app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.post('/', function (req, res) {
  res.send('Got a POST request');
});

app.put('/user', function (req, res) {
  res.send('Got a PUT request at /user');
});

app.delete('/user', function (req, res) {
  res.send('Got a DELETE request at /user');
});

app.listen(3000);
var express = require('express'),
    app = express();

var checkAuth = function(req, res, next) {
  var err = null;
  if(!isAuthenticated) {
    err = 'Unauthenticated';
  }
  next(err);
}

app.get('/', checkAuth, function (req, res) {
  res.send('Hello World!');
});

app.listen(3000);
npm install express

Async

Simplify asynchronous code, eliminate 'callback hell'

npm install async
var async = require('async');

async.waterfall([
  function(cb) {
    async1(input, cb);
  },
  function(result, cb) {
    async2(result, cb);
  },
  function(result, cb) {
    async3(result, cb);
  },
  function(result, cb) {
    async4(result, cb);
  },
], function(err, output) {
  if(err) { /* handle error */ }
  // do something with output
});
fn1(input, function(err, result1) {
  if(err) { /* handle error */ }
  fn2(result1, function(err, result2) {
    if(err) { /* handle error */ }
    fn3(result2, function(err, result3) {
      if(err) { /* handle error */ }
      fn4(result3, function(err, output) {
        if(err) { /* handle error */ }
        // do something with output
      });
    });
  });
});

After

Before

Q

Simplify asynchronous code, eliminate 'callback hell'

npm install q
var Q = require('q');

var promise = 
  Q(input)
    .then(async1)
    .then(async2)
    .then(async3)
    .then(async4);

promise.done(
  function(output) {
    // do something with output
  }, 
  function(err) {
    // handle error
  });
fn1(input, function(err, result1) {
  if(err) { /* handle error */ }
  fn2(result1, function(err, result2) {
    if(err) { /* handle error */ }
    fn3(result2, function(err, result3) {
      if(err) { /* handle error */ }
      fn4(result3, function(err, output) {
        if(err) { /* handle error */ }
        // do something with output
      });
    });
  });
});

After

Before

Lodash

Toolkit for manipulating objects and collections

npm install lodash
var _ = require('lodash');

var defaults = function(options) {
  return _.defaults(options, {
    createdAt: '2015-02-21'
  });
};

console.log(defaults({
  createdAt: '2015-02-16'
}));
// -> { createdAt: '2015-02-16' }

console.log(defaults({}));
// -> { createdAt: '2015-02-21' }
var _ = require('lodash');

var people = [
  { name: 'Frank', age: 30 },
  { name: 'Susan', age: 10 },
  { name: 'Bill',  age: 22 },
  { name: 'Carol', age: 52 },
  { name: 'Stef',  age: 22 }
];

var result = _(people)
    .filter({ age: 22 })
    .value();

console.log(result);
//  [ { name: 'Bill', age: 22 }
//    { name: 'Stef', age: 22 } ]

Defaults

Filtering

Request

Simplify sending HTTP requests

var request = require('request');

request(
  'http://www.google.com', 
  function(err, res, body) {
    if(err) {
    // Handle error

    } else if (res.statusCode === 200) {
      console.log(body);
    }
});
var http = require('http');

var options = {
  host: 'www.google.com'
};

callback = function(res) {
  var str = '';

  res.on('data', function (chunk) {
    str += chunk;
  });

  res.on('end', function () {
    console.log(str);
  });
};

var req = http.request(options, callback);

req.on('error', function(err) {
  // Handle error
});

req.end();
npm install request

After

Before

More...

User Authentication

Passport

Real-time web framework

Socket.io

Unit testing

Mocha

For building CLIs

Commander

Popular Libraries 
https://www.npmjs.com/browse/star

Schemas for MongoDB

Mongoose

Afternoon Schedule

10.00 - Start Workshops 
11.00 - Break 
11.30 - Workshops 
12.30 - Lunch 
13.30 - Presentation: Node libraries


13.45 - Workshops 
15.00 - Break 
15.30 - Workshops 
16.30 - Presentation: Five things you can do with Node
17:00 - Head to Big Chill Bar for drinks

Suggested Workshops

npm install -g async-you

Async

npm install -g expressworks

Express

npm install -g promise-it-wont-hurt

Promises

npm install -g lololodash

Lodash

Copy of nodeschool-libs

By Manoj Pandey

Copy of nodeschool-libs

Node libraries presentation for NodeSchool Bristol #1.

  • 1,664