Node presentation


Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Node presentation

  1. 1. Node.js + Express.js
  2. 2. What is Node.js? • Node is a web application platform built on top of the Chrome V8 engine. • It is event-driven, non-blocking and favors asynchronous operations. • Due to Node’s non-blocking nature it allows servers to handle a lot of concurrent users thereby making it scalable.
  3. 3. What is Express.js • Express.js is a web application framework, built for Node.js, which provides easy to use abstractions for URL/HTTP routing and views. • Express is by far the most popular framework for Node.js, some lesser-known alternatives include Geddy and Stack.
  4. 4. Node.js • Non-blocking and asynchronous - Here’s an analogy – Starbucks customer processing.
  5. 5. In Java you would write: public ArrayList<ArrayList<Likes> getFriendsLikes() { User currentUser = webapp.getCurrentUser(); ArrayList<Users> friendList = simpledb.getFriends(currentUser); ArrayList<Likes> likesList = new ArrayList<Likes>(); for(friend in friendList) { ArrayList<Likes> friendLikes = simpledb.getLikes(friend); likesList.addAll(friendLikes); } return likesList; } Write a method to get a list of things your friends like
  6. 6. In Node.js you can translate that literally into the function below (this is pseudocode!!) function getFriendsLikes() { var friendsLikesList = []; simpledb.getCurrentUser(function (currentUser) { simpledb.getFriends(currentUser, function (friendsList) { for(friend in friendsList) { simpledb.getLikes(friend, function(likesList) { friendsLikesList.addAll(likesList); } } app.render(JSON.stringify(friendsLikesList)); }); }); } Will this work?
  7. 7. Node is asynchronous and so porting Java logic directly will not work directly. The problem is that Node will run through the for-each loop and will execute the simpledb.getLikes(friend, function(likesList){ friendsLikesList.add(likesList)}); statements. However, we are only defining a callback here so Node will not block and wait for these callbacks to get called; instead, it will just keep executing the for-each loop for all the friends and then immediately execute the app.render(JSON.stringify(friendsLikesList)); There is no guarantee that all the simpledb.getLikes() methods will have received their respective responses by the time the return statement is executed and therefore the friendsLikesList returned will not contain all the likes of the user’s friends.
  8. 8. So what’s the solution? The simplest solution is to use a flow control module : async.js What async lets you do is it lets you provide a callback function that will get called when your for- each loop has finished running (It also lets you do a ton of other cool stuff). So, let’s look at what the solution looks like using Async (next slide)
  9. 9. var result = function getFriendsLikes() { var friendsLikesList = []; simpledb.getCurrentUser(function (currentUser) { simpledb.getFriends(currentUser, function (friendsList) { async.forEach(friendsList, function(friend, callback) { simpledb.getLikes(friend, function(likesList) { friendsLikesList.addAll(likesList); callback(); }); }, function(err) { callback(undefined, friendsLikesList); }); }); }); } result(function(err, friendsLikesList) { app.render(JSON.stringify(friendsLikesList)); }
  10. 10. Express.js