Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Web Services with MongoDB, Node.JS, and Openshift Webinar

mongodb
May 25, 2012
1.7k

Building Web Services with MongoDB, Node.JS, and Openshift Webinar

MongoDB and Node.JS are an excellent match. Server-side javascript FTW! We will discuss how to model a basic "to do list" application in MongoDB, and after modeling the data, we will map that to an application we build together in Node.JS. Finally, we'll deploy the application to OpenShift, Red Hat's Platform as a Service. We'll be left with a full fledged application running in the cloud!

mongodb

May 25, 2012
Tweet

Transcript

  1. • High Concurrency low latency • Evented I/O • Asynchronous

    programming platform • Single-threaded, no thread and context swaps • A really fast javascript platform Friday, May 25, 12
  2. • Fantastic combination • Both do Javascript • Both do

    JSON Natively • Both are asynchronous • Modern web is Javascript + JSON • Avoid mental context switching + Friday, May 25, 12
  3. • Build realtime web apps • High throughput and concurrency

    apps • Analytics/chat/cms you name it • Scale your application with less hardware and resources + Friday, May 25, 12
  4. Developing on Openshift • No Need to SSH into a

    Box • No Need to Manage CPU, Memory, etc • No Need to “start” the Server • No Need to fiddle with operating systems Friday, May 25, 12
  5. REST Web Service ToDo List • Should be able to

    create users • Should be able to create reminders for users • Should be able to view reminders • Should be able to update reminders • Should be able to delete reminders Friday, May 25, 12
  6. Pre-Reqs • A Terminal • mongoDB • Node.JS + NPM

    + Express • Red Hat Openshift Tools (RHC) • Web Browser • HttpRequester Firefox Extension (useful for testing Friday, May 25, 12
  7. Back to Node.JS var express = require('express'), Db = require('mongodb').Db,

    Server = require('mongodb').Server, Connection = require('mongodb').Connection; var host = 'localhost'; var port = Connection.DEFAULT_PORT; var db = new Db('node-mongo-examples', new Server(host, port, {}), {native_parser:false}); var app = express.createServer(); app.get('/', function(req, res){ res.send('Hello World'); }); db.open(function(err, db) { if(err) throw err app.listen(8124); }); Friday, May 25, 12
  8. Insert a Person app.put('/user/:name', function(req, res){ var document = {name:req.params.name};

    db.collection('names').insert(document,{safe:true},function(err,doc){ if(err){ console.log(err); res.send("Fail") } else{ res.header("Content-Type:","text/json"); res.end(JSON.stringify(doc[0]._id)); } }); }); Friday, May 25, 12
  9. Insert a Task app.put('/todos/:id/:date/:todo', function(req, res){ var document = {creator:req.params.id,date:req.params.date,todo:req.params.todo};

    db.collection('todos').insert(document,{safe:true},function(err,doc){ res.header("Content-Type:","text/json"); res.end(JSON.stringify(doc[0]._id)); }); }); Friday, May 25, 12
  10. Update a Task app.post('/todos/:id/:todo', function(req, res){ var id = ObjectID(req.params.id);

    db.collection('todos').update({_id:id},{$set:{todo:req.params.todo}}, {safe:true},function(err,doc){ if(err){ res.header("Content-Type:","text/json"); res.end(JSON.stringify({message:"fail"})); } else{ res.header("Content-Type:","text/json"); res.end(JSON.stringify({message:"success"})); } }); }); Friday, May 25, 12
  11. Get Tasks for a User app.get('/todos/:userid', function(req, res){ db.collection('todos').find({creator:req.params.userid}).toArray(function(err, todos)

    { res.header("Content-Type:","text/json"); res.end(JSON.stringify(todos)); }); }); Friday, May 25, 12
  12. Delete a Task app.delete('/todos/:id', function(req, res){ var id = ObjectID(req.params.id);

    db.collection('todos').remove({_id:id},{safe:true},function(err,doc){ res.header("Content-Type:","text/json"); res.end(JSON.stringify({message:"success"})); }) }); Friday, May 25, 12
  13. @mongodb http://bit.ly/mongo1   Facebook            

           |                  Twitter                  |                  LinkedIn http://linkd.in/joinmongo More info at http://www.mongodb.org/ I  will  post  the  slides  /  code  snippets  within  48  hours... @hungarianhc Questions?  E-­‐mail  me  at  [email protected] Friday, May 25, 12