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. 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.
6. 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
7. 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?
8.
9. 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.
10. 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)
11. 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));
}