SlideShare une entreprise Scribd logo
1  sur  12
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

Contenu connexe

Tendances

SockJS Intro
SockJS IntroSockJS Intro
SockJS Intro
Ngoc Dao
 

Tendances (16)

Servlets & jsp Overview
Servlets & jsp OverviewServlets & jsp Overview
Servlets & jsp Overview
 
Wordpress -> Middleman: Lesson learned in the 2-years since migrating
Wordpress -> Middleman: Lesson learned in the 2-years since migratingWordpress -> Middleman: Lesson learned in the 2-years since migrating
Wordpress -> Middleman: Lesson learned in the 2-years since migrating
 
Async Web QA
Async Web QAAsync Web QA
Async Web QA
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node js Introduction
Node js IntroductionNode js Introduction
Node js Introduction
 
Node js for beginners
Node js for beginnersNode js for beginners
Node js for beginners
 
DevOps in the era of serverless computing - Alessandro Vozza - Codemotion Ams...
DevOps in the era of serverless computing - Alessandro Vozza - Codemotion Ams...DevOps in the era of serverless computing - Alessandro Vozza - Codemotion Ams...
DevOps in the era of serverless computing - Alessandro Vozza - Codemotion Ams...
 
SockJS Intro
SockJS IntroSockJS Intro
SockJS Intro
 
Node.js introduction
Node.js introductionNode.js introduction
Node.js introduction
 
IBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New TricksIBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New Tricks
 
Spring + WebSocket integration
Spring + WebSocket integrationSpring + WebSocket integration
Spring + WebSocket integration
 
Best node js course
Best node js courseBest node js course
Best node js course
 
Continuous Integration on Steroids
Continuous Integration on SteroidsContinuous Integration on Steroids
Continuous Integration on Steroids
 
ReactJS.NET - Fast and Scalable Single Page Applications
ReactJS.NET - Fast and Scalable Single Page ApplicationsReactJS.NET - Fast and Scalable Single Page Applications
ReactJS.NET - Fast and Scalable Single Page Applications
 
Search and analyze your data with elasticsearch
Search and analyze your data with elasticsearchSearch and analyze your data with elasticsearch
Search and analyze your data with elasticsearch
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDD
 

Similaire à Node presentation

Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
Rob Davarnia
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
Mohammad Qureshi
 

Similaire à Node presentation (20)

Kalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect GuideKalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect Guide
 
Express node js
Express node jsExpress node js
Express node js
 
Node
NodeNode
Node
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
 
Node js
Node jsNode js
Node js
 
Proposal
ProposalProposal
Proposal
 
Introduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.com
 
AngularJS and SPA
AngularJS and SPAAngularJS and SPA
AngularJS and SPA
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Sinatra and JSONQuery Web Service
Sinatra and JSONQuery Web ServiceSinatra and JSONQuery Web Service
Sinatra and JSONQuery Web Service
 
Introduction to Node.JS
Introduction to Node.JSIntroduction to Node.JS
Introduction to Node.JS
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
 
Олександр Хотемський “ProtractorJS як інструмент браузерної автоматизації для...
Олександр Хотемський “ProtractorJS як інструмент браузерної автоматизації для...Олександр Хотемський “ProtractorJS як інструмент браузерної автоматизації для...
Олександр Хотемський “ProtractorJS як інструмент браузерної автоматизації для...
 
Major project report
Major project reportMajor project report
Major project report
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
 
GWT is Smarter Than You
GWT is Smarter Than YouGWT is Smarter Than You
GWT is Smarter Than You
 
Servlets & jdbc
Servlets & jdbcServlets & jdbc
Servlets & jdbc
 
NodeJS @ ACS
NodeJS @ ACSNodeJS @ ACS
NodeJS @ ACS
 
ExpressJS-Introduction.pdf
ExpressJS-Introduction.pdfExpressJS-Introduction.pdf
ExpressJS-Introduction.pdf
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Dernier (20)

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Node presentation

  • 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.
  • 4.
  • 5. Node.js • Non-blocking and asynchronous - Here’s an analogy – Starbucks customer processing.
  • 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)); }