• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Node presentation
 

Node presentation

on

  • 556 views

 

Statistics

Views

Total Views
556
Views on SlideShare
556
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Node presentation Node presentation Presentation Transcript

    • Node.js + Express.js
    • 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.
    • 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.
    • Node.js • Non-blocking and asynchronous - Here’s an analogy – Starbucks customer processing.
    • 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
    • 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?
    • 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.
    • So what’s the solution? The simplest solution is to use a flow control module : async.js https://github.com/caolan/async) 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)
    • 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)); }
    • Express.js