SlideShare a Scribd company logo
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary.
Express.js labs
Aeshan Wijetunge
06/ 11 / 2015 ITE
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 2
Middleware
A function with access to the request object, the response object, and the next
middleware in line in the request-response cycle of an application.
Middleware
Middleware
Application
Request Response
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 3
MVC : Model-View-Controller
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 4
Views: Picking a template engine
There are dozens of template engines available online…. But we’ll
settle on
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 5
DustJS : adding a template engine
Be sure to read-up on their awesome documentation when using dust.js
http://akdubya.github.io/dustjs/#guide
We’ll be adding dust-support using the adaro node-module. Installing it is as
easy as running...
npm install adaro --save
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 6
Update app.js with the dust view-engine
app.engine('dust', adaro.dust());
app.set('view engine', 'dust');
app.set('views', __dirname +'/public/views' );
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 7
Update app.js with a route
app.get('/home',function(req, res) {
req.model = {};
req.model.name = 'ITE';
res.render('index',req.model);
});
This renders a dust-template located :
/views/index.dust
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 8
Our 1st dust template
Save it under /public/views/index.dust
Let's get it running :
node --debug app.js
Visit your VM
aeshanw.koding.io:6001/home
<html>
<body>
<div>Hello {name}!</div>
</body>
</html>
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 9
Dustjs: under the hood
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 10
Challenge : A Weather App
We’re going to use a weather-api to fetch the latest updates for some cities of
our choice.
With a bit of googling we find a site with some open APIs (not requiring API
secret keys etc) which is great for our example
http://openweathermap.org/current
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 11
Mashups : Using APIs
We want to use this API to fetch the latest singapore weather details:
http://api.openweathermap.org/data/2.5/weather?q=Singapore
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 12
Weather API : the raw data
We used an online tool to make the API response
readable..
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 13
Request: the API client
npm install request --save
Googling for API clients will uncover many though we’ll be using
the request module to consume our weather API.
request('http://api.openweathermap.org/data/2.5/weather?q=Si
ngapore', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Weather Data for Singapore
}
})
Hitting your koding.io url should populate the console.log in your
koding terminal.
http://aeshanw.koding.io:6001/weather
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 14
Populate the view model
To display on the dust template let’s add the following to the app.js
app.get('/weather', function(req, res){
……..
//Populate the view model used to display
req.model = {};
req.model.temp = Math.floor(result.main.temp/10.0),
req.model.country = result.name,
req.model.weather = result.weather;
……...
res.render('weather',req.model);
}
});
});
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 15
View : Presenting the data
With a better understanding of the API’s data response we can
modify our dust template to render it in a more presentable form
to the user.
In public/views/weather.dust
<html>
<body>
<h1>Weather for {country}</h1>
{#weather}
<ul>
<li>{main}</li>
<li>{description}</li>
</ul>
{/weather}
</body>
</html>
/app.js
req.model.country = result.name,
req.model.wind = result.wind,
req.model.weather = result.weather;
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 16
The Finished Result
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 17
Try it yourself
The complete code for the lab is in the github repo:
https://github.com/node-workshop-ite/express-
weather-lab
git clone https://github.com/node-workshop-
ite/express-weather-lab.git
cd express-weather-lab
npm install
npm start
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 18
Troubleshooting ports
Some networks block ports aggressively. So if you
encounter such issues you may need to use
Modify this code in app.js to use port 80.
app.listen(600180, function() {
sudo service apache2 stop
sudo node app.js
visit aeshanw.koding.io/weather
© 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 19
Conclusion
● Use frameworks like Express to organize your development
● Add features using node modules via npm
● Extend your web app functionality via external APIs
If you liked ExpressJS, and desire more features we strongly suggest
you try Kraken.js. It originated from PayPal and has more advanced
features for more scalable web apps.
http://krakenjs.com/
Thank you & Happy Hacking!

More Related Content

PPTX
Sst hackathon express
PPTX
AWS Amplify | AWS Amplify Tutorial | What is AWS Amplify | AWS Amplify Datast...
PDF
Interactive watch faces
PPTX
React Meetup 2
PDF
How to use apolloJS on React ?
PDF
Learn Angular 9/8 In Easy Steps
PDF
React Native - Getting Started
PPTX
PSE - Epicor ERP & REST
Sst hackathon express
AWS Amplify | AWS Amplify Tutorial | What is AWS Amplify | AWS Amplify Datast...
Interactive watch faces
React Meetup 2
How to use apolloJS on React ?
Learn Angular 9/8 In Easy Steps
React Native - Getting Started
PSE - Epicor ERP & REST

What's hot (7)

PDF
Quick Way to work with Models and Alloy in Appcelerator Titanium
PPTX
AWS serverless infrastructure - Integration testing
PPTX
AWS DOs and DONTs
PDF
I Love APIs 2015: Continuous Integration the Virtuous Cycle
PDF
Serverless Spring - Sabby Anandan
PDF
React Components Lifecycle | React Tutorial for Beginners | ReactJS Training ...
PPTX
The monster under the bed - overengineering the cloud 2020 am week
Quick Way to work with Models and Alloy in Appcelerator Titanium
AWS serverless infrastructure - Integration testing
AWS DOs and DONTs
I Love APIs 2015: Continuous Integration the Virtuous Cycle
Serverless Spring - Sabby Anandan
React Components Lifecycle | React Tutorial for Beginners | ReactJS Training ...
The monster under the bed - overengineering the cloud 2020 am week
Ad

Similar to Ite express labs (20)

PDF
Hands-On Lab: Using CA Mobile Application Analytics REST APIs
PPTX
AEM and Sling
PPTX
AEM and Sling
PPT
Oracle mcs overview 1029
PPTX
Node.js primer for ITE students
PPTX
H2O World - Self Guiding Applications with Venkatesh Yadav
PDF
apidays LIVE Australia 2020 - Data with a Mission by Matt McLarty
PDF
apidays LIVE Paris - Data with a mission: a COVID-19 API case study by Matt M...
PDF
Node.js Workshop
PDF
Bangalore Cloud Foundry meetup - Mani
PPTX
Monitoring Cloud Native Apps on Pivotal Cloud Foundry with AppDynamics
PPTX
Azure APIM Presentation to understand about.pptx
PPTX
Kraken.js Lab Primer
PDF
SharePoint 2013 Hosted-Apps (On-Premises) - Infrastructure Setup
PPTX
Running your Spring Apps in the Cloud Javaone 2014
PDF
PLNOG15: The Power of the Open Standards SDN API’s - Mikael Holmberg
PPTX
Getting Started with Apache Geode
PDF
Hands-On Lab: Increase Velocity with the CA Performance Management OpenAPI ...
PDF
Application Discovery! The Gift That Keeps on Giving
PDF
Application Discovery! The Gift That Keeps on Giving
Hands-On Lab: Using CA Mobile Application Analytics REST APIs
AEM and Sling
AEM and Sling
Oracle mcs overview 1029
Node.js primer for ITE students
H2O World - Self Guiding Applications with Venkatesh Yadav
apidays LIVE Australia 2020 - Data with a Mission by Matt McLarty
apidays LIVE Paris - Data with a mission: a COVID-19 API case study by Matt M...
Node.js Workshop
Bangalore Cloud Foundry meetup - Mani
Monitoring Cloud Native Apps on Pivotal Cloud Foundry with AppDynamics
Azure APIM Presentation to understand about.pptx
Kraken.js Lab Primer
SharePoint 2013 Hosted-Apps (On-Premises) - Infrastructure Setup
Running your Spring Apps in the Cloud Javaone 2014
PLNOG15: The Power of the Open Standards SDN API’s - Mikael Holmberg
Getting Started with Apache Geode
Hands-On Lab: Increase Velocity with the CA Performance Management OpenAPI ...
Application Discovery! The Gift That Keeps on Giving
Application Discovery! The Gift That Keeps on Giving
Ad

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Spectral efficient network and resource selection model in 5G networks
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
KodekX | Application Modernization Development
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Approach and Philosophy of On baking technology
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
Machine learning based COVID-19 study performance prediction
Spectral efficient network and resource selection model in 5G networks
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25 Week I
KodekX | Application Modernization Development
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
Encapsulation_ Review paper, used for researhc scholars
Unlocking AI with Model Context Protocol (MCP)
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Approach and Philosophy of On baking technology

Ite express labs

  • 1. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. Express.js labs Aeshan Wijetunge 06/ 11 / 2015 ITE
  • 2. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 2 Middleware A function with access to the request object, the response object, and the next middleware in line in the request-response cycle of an application. Middleware Middleware Application Request Response
  • 3. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 3 MVC : Model-View-Controller
  • 4. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 4 Views: Picking a template engine There are dozens of template engines available online…. But we’ll settle on
  • 5. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 5 DustJS : adding a template engine Be sure to read-up on their awesome documentation when using dust.js http://akdubya.github.io/dustjs/#guide We’ll be adding dust-support using the adaro node-module. Installing it is as easy as running... npm install adaro --save
  • 6. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 6 Update app.js with the dust view-engine app.engine('dust', adaro.dust()); app.set('view engine', 'dust'); app.set('views', __dirname +'/public/views' );
  • 7. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 7 Update app.js with a route app.get('/home',function(req, res) { req.model = {}; req.model.name = 'ITE'; res.render('index',req.model); }); This renders a dust-template located : /views/index.dust
  • 8. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 8 Our 1st dust template Save it under /public/views/index.dust Let's get it running : node --debug app.js Visit your VM aeshanw.koding.io:6001/home <html> <body> <div>Hello {name}!</div> </body> </html>
  • 9. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 9 Dustjs: under the hood
  • 10. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 10 Challenge : A Weather App We’re going to use a weather-api to fetch the latest updates for some cities of our choice. With a bit of googling we find a site with some open APIs (not requiring API secret keys etc) which is great for our example http://openweathermap.org/current
  • 11. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 11 Mashups : Using APIs We want to use this API to fetch the latest singapore weather details: http://api.openweathermap.org/data/2.5/weather?q=Singapore
  • 12. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 12 Weather API : the raw data We used an online tool to make the API response readable..
  • 13. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 13 Request: the API client npm install request --save Googling for API clients will uncover many though we’ll be using the request module to consume our weather API. request('http://api.openweathermap.org/data/2.5/weather?q=Si ngapore', function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Weather Data for Singapore } }) Hitting your koding.io url should populate the console.log in your koding terminal. http://aeshanw.koding.io:6001/weather
  • 14. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 14 Populate the view model To display on the dust template let’s add the following to the app.js app.get('/weather', function(req, res){ …….. //Populate the view model used to display req.model = {}; req.model.temp = Math.floor(result.main.temp/10.0), req.model.country = result.name, req.model.weather = result.weather; ……... res.render('weather',req.model); } }); });
  • 15. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 15 View : Presenting the data With a better understanding of the API’s data response we can modify our dust template to render it in a more presentable form to the user. In public/views/weather.dust <html> <body> <h1>Weather for {country}</h1> {#weather} <ul> <li>{main}</li> <li>{description}</li> </ul> {/weather} </body> </html> /app.js req.model.country = result.name, req.model.wind = result.wind, req.model.weather = result.weather;
  • 16. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 16 The Finished Result
  • 17. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 17 Try it yourself The complete code for the lab is in the github repo: https://github.com/node-workshop-ite/express- weather-lab git clone https://github.com/node-workshop- ite/express-weather-lab.git cd express-weather-lab npm install npm start
  • 18. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 18 Troubleshooting ports Some networks block ports aggressively. So if you encounter such issues you may need to use Modify this code in app.js to use port 80. app.listen(600180, function() { sudo service apache2 stop sudo node app.js visit aeshanw.koding.io/weather
  • 19. © 2015 PayPal Inc. All rights reserved. Confidential and proprietary. 19 Conclusion ● Use frameworks like Express to organize your development ● Add features using node modules via npm ● Extend your web app functionality via external APIs If you liked ExpressJS, and desire more features we strongly suggest you try Kraken.js. It originated from PayPal and has more advanced features for more scalable web apps. http://krakenjs.com/ Thank you & Happy Hacking!