build a


HTTP WEB API
          w/ nodejs & mongodb
tweet tweet @donnfelker
github.com/donnfelker/workout-tracker
agenda


         nodejs
         express
         mongoose
         mongodb
beginner | intermediate
what is   ?
Node.js is a platform built on Chrome's JavaScript runtime
for easily building fast, scalable network applications.

Node.js uses an event-driven, non-blocking I/O model that
makes it lightweight and efficient, perfect for data-intensive
real-time applications that run across distributed devices.

                                            -nodejs.org
standing on the shoulders of giants
how do I build a web api with nodejs?
basic tools   blank slate
expressjs.com




express
web application
framework for
node
express()
Create an express application.
var express = require('express');
var app = express();

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

app.listen(3000);
express()
Create an express application.
var express = require('express');
var app = express();

app.get('/', function(req, res){
  res.json(200, { message : “Hi!” });
});

app.listen(3000);
persistence
what is mongoDB ?
MongoDB (from "humongous") is a scalable, high-
performance, open source NoSQL database.


                               - mongodb.org
records are stored known as ‘documents’ and resemble json

      {
          "_id": "4ff09aabf2c99f6cac000006",
          "description": "21-15-9 of Thrusters and Pull Ups",
          "name": "Fran",
          "date_created": "2012-07-01T18:44:59.580Z"
      }
express
 nodejs

mongoose
mongodb
mongoosejs.com




mongoose
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'workout_tracker');

var schema = mongoose.Schema({ name: 'string' });
var Workout = db.model('workout', schema);

var w = new Workout({ name: 'Fran' });
w.save(function (err) {
  if (err) // ...
  res.end('meow');
});
building the web api
new express project

create a folder for the app    mkdir workout-tracker



create a package.json file      {
      with these contents          "name": "workout-tracker",
                                   "description": "workout tracker app",
                                   "version": "0.0.1",
                                   "private": true,
                                   "dependencies": {
                                     "express": "3.x"
                                   }
                               }




 install dependencies and      $ npm install
             install express
$ npm ls
workout-tracker@0.0.1 /private/tmp
!"# express@3.0.0beta7
  $"" commander@0.6.1
  $"# connect@2.3.9
  % $"" bytes@0.1.0
  % $"" cookie@0.0.4
  % $"" crc@0.2.0
  % $"" formidable@1.0.11
  % !"" qs@0.4.2
  $"" cookie@0.0.3
  $"" debug@0.7.0
  $"" fresh@0.1.0
  $"" methods@0.0.1
  $"" mkdirp@0.3.3
  $"" range-parser@0.0.4
  $"# response-send@0.0.1
  % !"" crc@0.2.0
  !"# send@0.0.3
    !"" mime@1.2.6
ok stop - code time
learn js
the right way
code      github.com/donnfelker/workout-tracker

twitter   @donnfelker

          MongoHub

Building HTTP API's with NodeJS and MongoDB