Your SlideShare is downloading. ×
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Intro to node and mongodb   1
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Intro to node and mongodb 1

1,281

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,281
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
24
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Introduction toM. Khurrum QureshiSr. Software Engineer
  • 2. Node’s Goal is to provide aneasy way to build scalablenetwork programs.
  • 3. Node.js is NOT anotherweb framework!But you can create a web framework using NPM modules.
  • 4. Node.js is…Web ServerTCP ServerAwesome Robot ControllerCommand Line ApplicationProxy ServerStreaming ServerVoiceMail ServerMusic MachineAnything that has to deal with high I/O
  • 5. Node.js isServer Side JavaScript!
  • 6. Node.js isFUN!
  • 7. Why Node.js?• Non Blocking I/O• Based on Chrome’s V8 Engines (FAST!)• 15,000+ Modules• Active Community (IRC, Mailing Lists, Twitter,Github)• Mac, Linux and Windows (all first class citizens)• One Language for Frontend and Backend• JavaScript is the Language of the Web
  • 8. Node.js Good Use Cases• JSON APIsBuilding light-weight REST / JSON apis is something where node.jsreally shines. Its non-blocking I/O model combined with JavaScriptmake it a great choice for wrapping other data sources such asdatabases or web services and exposing them via a JSON interface.• Single Page AppsIf you are planning to write an AJAX heavy single page app (thinkgmail), node.js is a great fit as well. The ability to process manyrequests / seconds with low response times, as well as sharing thingslike validation code between the client and server make it a greatchoice for modern web applications that do lots of processing on theclient.
  • 9. Node.js Good Use Cases• Shelling out to Unix ToolsWith node.js still being young, its tempting to re-invent all kinds ofsoftware for it. However, an even better approach is tapping into thevast universe of existing command line tools. Nodes ability to spawnthousands of child processes and treating their outputs as a streammakes it an ideal choice for those seeking to leverage existingsoftware.• Streaming DataTraditional web stacks often treat http requests and responses asatomic events. However, the truth is that they are streams, and manycool node.js applications can be built to take advantage of this fact.One great example is parsing file uploads in real time, as well asbuilding proxies between different data layers.
  • 10. Node.js Good Use Cases• Soft Real Time ApplicationsAnother great aspect of node.js is the ease at which you can developsoft real time systems. By that I mean stuff like twitter, chat software,sport bets or interfaces to instant messaging networks.
  • 11. Basic HTTP Servervar http = require(http);var server = http.createServer(function (req, res) {res.writeHead(200);res.end(Hello World);});server.listen(4000);
  • 12. Some people use thecore http module tobuild their web apps,most use a frameworklike Expressor Connect or Flatiron or Tako or Derby or Geddy or Mojito or …
  • 13. Visithttp://expressjs.com/guide.htmlfor a detailed guideon using Express
  • 14. What is Non-Blocking I/O?And why should I care?
  • 15. Blocking I/270ms = SUM(user, activities, leaderboard)// Get User – 20ms$query = SELECT * FROM users WHERE id = ?;$users = query($query, array($id));print_r($users);// Get Activities – 100ms$query = SELECT * FROM activities ORDER BY timestamp LIMIT 50;$activities = query($query);print_r($activities);// Get Leader Board – 150ms$query = SELECT count(points) as total, user_id FROM activities LIMIT 50;$leader_board = query($query);
  • 16. Non-Blocking I/150ms = MAX(user, activities, leaderboard)// Get User – 20msvar query = SELECT * FROM users WHERE id = ?;db.query(query, [userId], function (err, results) {console.log(results);});// Get Activities – 100msvar query = SELECT * FROM activities ORDER BY timestamp LIMIT 50;db.query(query, function (err, results) {console.log(results);});// Get Leader Board – 150msvar query = SELECT count(points) as total, user_id FROM activities LIMIT 50;db.query(query, function (err, results) {console.log(results);});
  • 17. The most jarring thingabout Server Side JavaScriptis thinking in callbacks
  • 18. The Node Callback PatternawesomeFunction(arg, function (err, data) {if (err) {// Handle Error}// Do something awesome with results.});• Error first then success… ALWAYS!• Because this is the de-facto standard 99.99999% of the timeyou will be able to guess how a Node library will work.
  • 19. Callbacks are the Devil’s Work!Don’t go down this rabbit hole…One of the biggest mistakes is to get yourself into callback hell by nesting callbacks inside ofcallbacks inside of more callbacks.var userQuery = SELECT * FROM users WHERE id = ?;var activityQuery = SELECT * FROM activities ORDER BY timestamp LIMIT 50;var leaderBoardQuery = SELECT count(points) as total, user_id FROM activities LIMIT 50;db.query(userQuery, [id], function (userErr, userResults) {db.query(activityQuery, function (activityErr, activityResults) {db.query(leaderBoardQuery, function (leaderBoardErr, leaderBoardResults) {// Do something here});});});
  • 20. Avoiding Callback Hell• Keep your code shallow• Break up your code into small chunks• Use a sequential library like async• Visit http://callbackhell.com
  • 21. Async to the rescue!var async = require(async);var db = require(’db);function getUser (callback) {var query = SELECT * FROM users WHERE id = ?;db.query(query, [userId], callback);}function getActivities (callback) {var query = SELECT * FROM activities ORDER BY timestamp LIMIT 50;db.query(query, callback);}function getLeaderBoard (callback) {var query = SELECT count(points) as total, user_id FROM activities LIMIT 50;db.query(query, callback);}var tasks = [getUser, getActivities, getLeaderBoard];async.parallel(tasks, function (err, results) {var user = results[0];var activities = results[1];var leaderBoard = results[2];});
  • 22. Visithttps://github.com/caolan/asyncfor a detailed guide on using the async module.Async provides several usefulpatterns for asynchronous controlflow including: parallel, series,waterfall, auto and queue.
  • 23. The Node Package Managerotherwise know as… NPMIt’s how you harness theawesomeness of theNode.js community!
  • 24. Using NPMIt’s standard practice to install modules locally for your current project.Modules are installed in the ./node_modules in the current directory.To Install a new modulenpm install <module>To find a module in the NPM repositorynpm search <search string>To list the modules (and their dependencies) in the current projectnpm listTo see module detailsnpm info <module>
  • 25. DON’T INSTALLMODULES GLOBALLY!Unless they are tools like node-dev, jake, express, minify-jsOR linked development modules but more on that later.
  • 26. NPM is awesome sauce!Visithttps://npmjs.orgfor more details about NPM and tobrowse the current NPM Repository
  • 27. Node.js Modules• async• connect• express• mongodb-native-driver• request• apn• ql.io-engine• pem• winston• winston-mongodb
  • 28. Node.js Modules• node-sql• nodemailer• connect-http-signature• http-signature• underscore• file-utils• validator• mongoskin• passport.Js• yql
  • 29. Node.js Modules• node-gcm• forever• mongodb_s3_backup• nconf• node-sqlserver• Socket-io• generic-pool• And manyothers
  • 30. Deployment Platforms• Amazon EC2• Windows Azure• Heroku• Joynet• Nodejistu
  • 31. Questions?
  • 32. Introduction to
  • 33. NoSql• Non-Relational• Horizontally Scalable• Distributed• Schema-Free• Open-Source• Replication Support• Simple API
  • 34. NoSql Flavours• Key-value Store.• Graph• Big Table• Document Store
  • 35. mongoDB Overview• Document Database– Documents (objects) map nicely to programming language data types.– Embedded documents and arrays reduce need for joins.– Dynamic schema• High Performance– Embedding makes reads and writes fast.– Indexes can include keys from embedded documents and arrays.– Optional streaming writes (no acknowledgments).• High Availability– Replicated servers with automatic master failover.
  • 36. mongoDB Overview• Easy Scalability– Automatic sharding distributes collection data across machines.
  • 37. mongoDB Data Model• MongoDB instance hosts a number of databases.• A database holds a set of collections.• A collection holds a set of documents.• A document is a set of key-value pairs.• Documents have dynamic schema.
  • 38. Document Structure• Data is stored in the form of JSON data.(Whichinternally stored as BSON){"_id" : ObjectId("4ccbfc75bd163019417c27f8"),"title": “Hello World! ","author": {"firstname": "Joe","lastname": "Bloggs"},"tags": ["test", "foo", "bar"]}
  • 39. Key mongoDB Features• Flexibility• Power• Speed/Scaling• Ease of use
  • 40. Questions?

×