SlideShare a Scribd company logo
1 of 27
UNTANGLING THE WEB
FALL 2017 WEEK 9 –JSON, REST API’S, MOVING YOUR WEBSITE TO BLUEMIX,
PROJECT 3
AGENDA
• Homework discussion
• Running locally
• Security
• .gitignore
• JSON
• RESTful APIs
• Moving your project 2 to Bluemix
• Project 3
• Homework 9
AGENDA (SCRATCH)
• Homework discussion – go over joins, talk about it in detail
• Running locally
• Security – getting credentials, creating vcap-local.json
• .gitignore – vcap, node_modules
• Npm install, npm start
• Looking at output logs
• Looking at console log in client
• Show json in console log
• JSON – talk about how json is structured. Go over helper functions for parsing json in node. Prettify, parse, etc.
• RESTful APIs – go grab REST section from last term
• Moving your project 2 to Bluemix -
• Project 3
• Homework 9
HOMEWORK 8
• Submit a github link with a file containing the query and results for each question
• Using the stackoverflow database here: http://data.stackexchange.com/stackoverflow/queries
• List the oldest 10 Victoria, BC users and order them by age.
• List the top 10 highest reputation users in Victoria that have answered questions with the tag javascript.
• List the top 5 Victoria developers by accepted answer percentage
• List the top 10 Victoria developers by accepted answer percentage on questions with the tag javascript
SQL QUERY PROBLEMS
• Look over queries here:
• http://data.stackexchange.com/stackoverflow/queries
• Look on SQLZoo for the JOINS sections and make sense of that
• http://sqlzoo.net/wiki/The_JOIN_operation
• More about joins
• https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
AN APPROACH TO QUESTION 4
• SELECT TOP 10
• Users.Id,
• DisplayName,
• Location,
• Count(Posts.Id) AS Answers,
• AVG(CAST(Score AS float)) AS [Percentage Accepted]
• FROM Posts
• INNER JOIN Users ON Users.Id = OwnerUserId
• WHERE Location like '%##Location##%'
• AND Posts.Tags like '%##Tags##%'
• GROUP BY Users.Id, DisplayName, Location
• HAVING Count(Posts.Id) > 5
• ORDER BY [Percentage Accepted] DESC
A MORE ELEGANT APPROACH
• SELECT TOP 10
• Users.Id,
• Users.DisplayName,
• Users.Location,
• ROUND((CAST(SUM(CASE WHEN a.Id=q.AcceptedAnswerId THEN 1 ELSE 0 END) AS float) / COUNT(a.Id)) * 100, 1) AS AcceptedAnswerPercentage
• FROM Users
• JOIN Posts a ON a.OwnerUserId = Users.Id and a.PostTypeId = 2
• JOIN Posts q ON a.ParentId = q.Id
• WHERE Users.Location = 'Victoria, BC'
• AND q.Tags LIKE '%javascript%'
• GROUP BY Users.Id, Users.DisplayName, Users.Location
• ORDER BY [AcceptedAnswerPercentage] DESC
JSON
• JSON stands for JavaScript Object Notation
• JSON is a lightweight data-interchange format
• JSON is "self-describing" and easy to understand
• JSON is language independent *
• https://www.w3schools.com/js/js_json.asp
JSON
• Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by any
programming language.
• JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects:
• JSON.parse()
• So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.
• (conversely, JSON.stringify() will go from object to string)
JSON IN THE CARS SQL EXAMPLE
• https://jsfiddle.net/m5rh3a83/1/
• (look at the results set in the console output)
CLOUDANT IS BASICALLY A JSON STORE
• IBM® Cloudant® is a managed NoSQL JSON database service built to ensure that the flow of data
between an application and its database remains uninterrupted and highly performant. Developers are
then free to build more, grow more and sleep more.
LOOK AT CLOUDANT DATA
• Recall the demo app from last week, let’s look at the objects that represent the images
• (through console.ng.bluemix.net, click on a database, then launch the tool)
LOOK AT APP.JS FOR AN EXAMPLE OF STRINGIFY
• var responseData = createResponseData( doc.id, docName,
docDesc, []);
• docList.push(responseData);
• response.write(JSON.stringify(docList));
• console.log(JSON.stringify(docList));
• console.log('ending response...’);
LOOK TO APP.JS FOR AN EXAMPLE OF PARSE
• function getDBCredentialsUrl(jsonData) {
• var vcapServices = JSON.parse(jsonData);
REST API
• An architectural style called REST (Representational State Transfer) advocates that web applications should use
HTTP as it was originally envisioned. Lookups should use GET requests. PUT, POST, and DELETE requests should
be used for mutation, creation, and deletion respectively.
• Keep in mind that GET requests should never be used for updating information. For example, a GET request
for adding an item to a cart
• http://myserver.com/addToCart?cart=314159&item=1729
• would not be appropriate. GET requests should be idempotent. That is, issuing a request twice should be no
different from issuing it once. That’s what makes the requests cacheable. An “add to cart” request is not
idempotent—issuing it twice adds two copies of the item to the cart. A POST request is clearly appropriate in
this context. Thus, even a RESTful web application needs its share of POST requests.
• (https://stackoverflow.com/questions/671118/what-exactly-is-restful-programming)
CLOUDANT REST API
• We’ll hide this detail behind an NPM package
• But it is possible to access cloudant just through the REST API
• https://developer.ibm.com/streamsdev/docs/integrating-with-cloudant-and-many-other-restful-
services/
NODEJS LOCAL SETUP
• You’ve all installed nodejs already, but haven’t used it for much
• Simplest version: https://code.visualstudio.com/docs/nodejs/nodejs-tutorial
• We’ll talk about how to run the bluemix example locally, though
SET UP LOCAL SECURITY
• Vcap-local.json
• Go get it from the bluemix console and save it in the project
• Make sure to add it to .gitignore!
GITIGNORE
• .gitignore is a file that tells git not to add certain files to your repository. Chief among these are any files
that have security credentials. Since your git repo is public you don’t want these up there and for some
credentials, like AWS keys, there are automated processes that scan public github repositories to steal
them
• Can also put other things you don’t need to save in there, for instance log files and the node_modules
directory
NPM
• Npm install is the first thing you’ll have to do, that brings in all the dependencies
• Then npm start
ADDING ANOTHER ROUTE AND VIEW
• Walk through the changes to app.js, adding an html page, and adding the route
RUNNING LOCALLY AND DEBUGGING
• We’ll cover actual debuggers later, but now there is a new place for console.log messages to go!
EJS
• EJS is a simple templating engine
• It is used for things like headers and footers, but also to pass variables from node into your html
• This way you can do data operations on the server side and just display on the client side
• Good tutorial: https://scotch.io/tutorials/use-ejs-to-template-your-node-application
• This is also a good resource: http://www.embeddedjs.com/getting_started.html
PROJECT WORK
• Possibilities:
• Look through the EJS tutorial and familiarize yourself with it. You will want to understand this for next week.
• Get a basic page up on bluemix and understand how to add routes, etc.
• Work on putting other things up to your cloudant database – understand the JSON format and the calls to
populate the database in app.js
• Get your project 2 pages up on bluemix (although not necessarily hooked up yet, the pages themselves should
be up there for this week’s homework.) As a stretch goal, break any headers and footers up into EJS partials and
get them to display
PROJECT 3
• You’ll need to implement a database backend for your project
• Database should have new item, search, and delete (optionally edit) from the ui
• You’ll need to implement a chatbot for your project
• Is it intuitive to use? Does it do something useful? Does it have good coverage?
• You’ll need to host your project on bluemix
• You’ll need to show a working demo of your project 2 hooked up to these backend services
PROJECT 3 GRADING
• All project 3 presentation are due on November 29th in class, late projects will not be accepted
• If you are horribly ill and nobody in your group can come in to present, the project itself is still due and the
presentation marks will be forfeit, or if absolutely necessary you can send me a demo video
• 15 points total
• 4 points presentation
• Is the demo good, is the website working, is the story about why it is interesting smooth
• 4 points database design and function (do things actually work)
• What is your table structure like? Talk about any problems you ran into. How was connecting it to the front end?
• 3 points chatbot implementation – is it effectively designed and well implemented?
• 4 points code quality and robustness
• Not everything has to work, but if it is there I want it to do nothing at all or the right thing. How have you structured your code? Is
it commented and easy to understand? Everything checked into github and deployed to the server? Good commit messages?
HOMEWORK 9 (DUE NOV 8 BEFORE CLASS)
• (can be done as a project group)
• Take your project 2 website and port it up to Bluemix (welcome to make changes, but not required to)
• Make sure that the github project is based off of the nodejs cloudant starter we discussed in class
• Submit a link to the site running on Bluemix and a link to your github for the group
• (to be done individually)
• Get the site running locally on your machine so that you can efficiently make changes
• Send me an email with a screenshot of the site running on your local machine

More Related Content

What's hot

What's hot (20)

Untangling spring week12
Untangling spring week12Untangling spring week12
Untangling spring week12
 
Untangling fall2017 week1
Untangling fall2017 week1Untangling fall2017 week1
Untangling fall2017 week1
 
Untangling spring week4
Untangling spring week4Untangling spring week4
Untangling spring week4
 
Untangling spring week8
Untangling spring week8Untangling spring week8
Untangling spring week8
 
Untangling the web week 2 - SEO
Untangling the web week 2 - SEOUntangling the web week 2 - SEO
Untangling the web week 2 - SEO
 
Untangling7
Untangling7Untangling7
Untangling7
 
Untangling fall2017 week2
Untangling fall2017 week2Untangling fall2017 week2
Untangling fall2017 week2
 
Untangling spring week2
Untangling spring week2Untangling spring week2
Untangling spring week2
 
Engage 2019: Modernising Your Domino and XPages Applications
Engage 2019: Modernising Your Domino and XPages Applications Engage 2019: Modernising Your Domino and XPages Applications
Engage 2019: Modernising Your Domino and XPages Applications
 
Why use Go for web development?
Why use Go for web development?Why use Go for web development?
Why use Go for web development?
 
A Personal Journey
A Personal JourneyA Personal Journey
A Personal Journey
 
Untangling - fall2017 - week6
Untangling - fall2017 - week6Untangling - fall2017 - week6
Untangling - fall2017 - week6
 
Dev112 let's calendar that
Dev112   let's calendar thatDev112   let's calendar that
Dev112 let's calendar that
 
Untangling - fall2017 - week5
Untangling - fall2017 - week5Untangling - fall2017 - week5
Untangling - fall2017 - week5
 
Engage 2019 Software documentation is fun if you have the right tools: Introd...
Engage 2019 Software documentation is fun if you have the right tools: Introd...Engage 2019 Software documentation is fun if you have the right tools: Introd...
Engage 2019 Software documentation is fun if you have the right tools: Introd...
 
Modern javascript
Modern javascriptModern javascript
Modern javascript
 
APIs: A Better Alternative to Page Objects
APIs: A Better Alternative to Page ObjectsAPIs: A Better Alternative to Page Objects
APIs: A Better Alternative to Page Objects
 
Untangling6
Untangling6Untangling6
Untangling6
 
How NOT to get lost in the current JavaScript landscape
How NOT to get lost in the current JavaScript landscapeHow NOT to get lost in the current JavaScript landscape
How NOT to get lost in the current JavaScript landscape
 
DEV117 - Unleash the Power of the AppDev Pack and Node.js in Domino
DEV117 - Unleash the Power of the AppDev Pack and Node.js in DominoDEV117 - Unleash the Power of the AppDev Pack and Node.js in Domino
DEV117 - Unleash the Power of the AppDev Pack and Node.js in Domino
 

Similar to Untangling - fall2017 - week 9

Web Development using Ruby on Rails
Web Development using Ruby on RailsWeb Development using Ruby on Rails
Web Development using Ruby on Rails
Avi Kedar
 

Similar to Untangling - fall2017 - week 9 (20)

Agile Secure Cloud Application Development Management
Agile Secure Cloud Application Development ManagementAgile Secure Cloud Application Development Management
Agile Secure Cloud Application Development Management
 
Advanced Site Studio Class, June 18, 2012
Advanced Site Studio Class, June 18, 2012Advanced Site Studio Class, June 18, 2012
Advanced Site Studio Class, June 18, 2012
 
Web Development using Ruby on Rails
Web Development using Ruby on RailsWeb Development using Ruby on Rails
Web Development using Ruby on Rails
 
How to Contribute to Apache Usergrid
How to Contribute to Apache UsergridHow to Contribute to Apache Usergrid
How to Contribute to Apache Usergrid
 
Managing Changes to the Database Across the Project Life Cycle (presented by ...
Managing Changes to the Database Across the Project Life Cycle (presented by ...Managing Changes to the Database Across the Project Life Cycle (presented by ...
Managing Changes to the Database Across the Project Life Cycle (presented by ...
 
Managing changes to eZPublish Database
Managing changes to eZPublish DatabaseManaging changes to eZPublish Database
Managing changes to eZPublish Database
 
Power of Azure Devops
Power of Azure DevopsPower of Azure Devops
Power of Azure Devops
 
Bringing DevOps to the Database
Bringing DevOps to the DatabaseBringing DevOps to the Database
Bringing DevOps to the Database
 
Why BaaS is crucial to early stage startups
Why BaaS is crucial to early stage startupsWhy BaaS is crucial to early stage startups
Why BaaS is crucial to early stage startups
 
Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1Code for Startup MVP (Ruby on Rails) Session 1
Code for Startup MVP (Ruby on Rails) Session 1
 
Desktop apps with node webkit
Desktop apps with node webkitDesktop apps with node webkit
Desktop apps with node webkit
 
Starting from scratch in 2017
Starting from scratch in 2017Starting from scratch in 2017
Starting from scratch in 2017
 
Creating a Documentation Portal
Creating a Documentation PortalCreating a Documentation Portal
Creating a Documentation Portal
 
Surrogate dependencies (in node js) v1.0
Surrogate dependencies  (in node js)  v1.0Surrogate dependencies  (in node js)  v1.0
Surrogate dependencies (in node js) v1.0
 
MongoDB Basics
MongoDB BasicsMongoDB Basics
MongoDB Basics
 
DevOps, Continuous Integration and Deployment on AWS: Putting Money Back into...
DevOps, Continuous Integration and Deployment on AWS: Putting Money Back into...DevOps, Continuous Integration and Deployment on AWS: Putting Money Back into...
DevOps, Continuous Integration and Deployment on AWS: Putting Money Back into...
 
Devops continuousintegration and deployment onaws puttingmoneybackintoyourmis...
Devops continuousintegration and deployment onaws puttingmoneybackintoyourmis...Devops continuousintegration and deployment onaws puttingmoneybackintoyourmis...
Devops continuousintegration and deployment onaws puttingmoneybackintoyourmis...
 
Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20
Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20
Continuous Integration with Open Source Tools - PHPUgFfm 2014-11-20
 
The Power of Azure DevOps
The Power of Azure DevOpsThe Power of Azure DevOps
The Power of Azure DevOps
 
WebDev Crash Course
WebDev Crash CourseWebDev Crash Course
WebDev Crash Course
 

More from Derek Jacoby (12)

Untangling the web - fall2017 - class 4
Untangling the web - fall2017 - class 4Untangling the web - fall2017 - class 4
Untangling the web - fall2017 - class 4
 
Untangling the web fall2017 class 3
Untangling the web fall2017 class 3Untangling the web fall2017 class 3
Untangling the web fall2017 class 3
 
Untangling fall2017 week2_try2
Untangling fall2017 week2_try2Untangling fall2017 week2_try2
Untangling fall2017 week2_try2
 
Untangling spring week7
Untangling spring week7Untangling spring week7
Untangling spring week7
 
Untangling spring week6
Untangling spring week6Untangling spring week6
Untangling spring week6
 
Untangling spring week3
Untangling spring week3Untangling spring week3
Untangling spring week3
 
Untangling spring week1
Untangling spring week1Untangling spring week1
Untangling spring week1
 
Untangling the web11
Untangling the web11Untangling the web11
Untangling the web11
 
Untangling the web10
Untangling the web10Untangling the web10
Untangling the web10
 
Untangling the web9
Untangling the web9Untangling the web9
Untangling the web9
 
Untangling8
Untangling8Untangling8
Untangling8
 
Untangling4
Untangling4Untangling4
Untangling4
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Untangling - fall2017 - week 9

  • 1. UNTANGLING THE WEB FALL 2017 WEEK 9 –JSON, REST API’S, MOVING YOUR WEBSITE TO BLUEMIX, PROJECT 3
  • 2. AGENDA • Homework discussion • Running locally • Security • .gitignore • JSON • RESTful APIs • Moving your project 2 to Bluemix • Project 3 • Homework 9
  • 3. AGENDA (SCRATCH) • Homework discussion – go over joins, talk about it in detail • Running locally • Security – getting credentials, creating vcap-local.json • .gitignore – vcap, node_modules • Npm install, npm start • Looking at output logs • Looking at console log in client • Show json in console log • JSON – talk about how json is structured. Go over helper functions for parsing json in node. Prettify, parse, etc. • RESTful APIs – go grab REST section from last term • Moving your project 2 to Bluemix - • Project 3 • Homework 9
  • 4. HOMEWORK 8 • Submit a github link with a file containing the query and results for each question • Using the stackoverflow database here: http://data.stackexchange.com/stackoverflow/queries • List the oldest 10 Victoria, BC users and order them by age. • List the top 10 highest reputation users in Victoria that have answered questions with the tag javascript. • List the top 5 Victoria developers by accepted answer percentage • List the top 10 Victoria developers by accepted answer percentage on questions with the tag javascript
  • 5. SQL QUERY PROBLEMS • Look over queries here: • http://data.stackexchange.com/stackoverflow/queries • Look on SQLZoo for the JOINS sections and make sense of that • http://sqlzoo.net/wiki/The_JOIN_operation • More about joins • https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/
  • 6. AN APPROACH TO QUESTION 4 • SELECT TOP 10 • Users.Id, • DisplayName, • Location, • Count(Posts.Id) AS Answers, • AVG(CAST(Score AS float)) AS [Percentage Accepted] • FROM Posts • INNER JOIN Users ON Users.Id = OwnerUserId • WHERE Location like '%##Location##%' • AND Posts.Tags like '%##Tags##%' • GROUP BY Users.Id, DisplayName, Location • HAVING Count(Posts.Id) > 5 • ORDER BY [Percentage Accepted] DESC
  • 7. A MORE ELEGANT APPROACH • SELECT TOP 10 • Users.Id, • Users.DisplayName, • Users.Location, • ROUND((CAST(SUM(CASE WHEN a.Id=q.AcceptedAnswerId THEN 1 ELSE 0 END) AS float) / COUNT(a.Id)) * 100, 1) AS AcceptedAnswerPercentage • FROM Users • JOIN Posts a ON a.OwnerUserId = Users.Id and a.PostTypeId = 2 • JOIN Posts q ON a.ParentId = q.Id • WHERE Users.Location = 'Victoria, BC' • AND q.Tags LIKE '%javascript%' • GROUP BY Users.Id, Users.DisplayName, Users.Location • ORDER BY [AcceptedAnswerPercentage] DESC
  • 8. JSON • JSON stands for JavaScript Object Notation • JSON is a lightweight data-interchange format • JSON is "self-describing" and easy to understand • JSON is language independent * • https://www.w3schools.com/js/js_json.asp
  • 9. JSON • Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by any programming language. • JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects: • JSON.parse() • So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object. • (conversely, JSON.stringify() will go from object to string)
  • 10. JSON IN THE CARS SQL EXAMPLE • https://jsfiddle.net/m5rh3a83/1/ • (look at the results set in the console output)
  • 11. CLOUDANT IS BASICALLY A JSON STORE • IBM® Cloudant® is a managed NoSQL JSON database service built to ensure that the flow of data between an application and its database remains uninterrupted and highly performant. Developers are then free to build more, grow more and sleep more.
  • 12. LOOK AT CLOUDANT DATA • Recall the demo app from last week, let’s look at the objects that represent the images • (through console.ng.bluemix.net, click on a database, then launch the tool)
  • 13. LOOK AT APP.JS FOR AN EXAMPLE OF STRINGIFY • var responseData = createResponseData( doc.id, docName, docDesc, []); • docList.push(responseData); • response.write(JSON.stringify(docList)); • console.log(JSON.stringify(docList)); • console.log('ending response...’);
  • 14. LOOK TO APP.JS FOR AN EXAMPLE OF PARSE • function getDBCredentialsUrl(jsonData) { • var vcapServices = JSON.parse(jsonData);
  • 15. REST API • An architectural style called REST (Representational State Transfer) advocates that web applications should use HTTP as it was originally envisioned. Lookups should use GET requests. PUT, POST, and DELETE requests should be used for mutation, creation, and deletion respectively. • Keep in mind that GET requests should never be used for updating information. For example, a GET request for adding an item to a cart • http://myserver.com/addToCart?cart=314159&item=1729 • would not be appropriate. GET requests should be idempotent. That is, issuing a request twice should be no different from issuing it once. That’s what makes the requests cacheable. An “add to cart” request is not idempotent—issuing it twice adds two copies of the item to the cart. A POST request is clearly appropriate in this context. Thus, even a RESTful web application needs its share of POST requests. • (https://stackoverflow.com/questions/671118/what-exactly-is-restful-programming)
  • 16. CLOUDANT REST API • We’ll hide this detail behind an NPM package • But it is possible to access cloudant just through the REST API • https://developer.ibm.com/streamsdev/docs/integrating-with-cloudant-and-many-other-restful- services/
  • 17. NODEJS LOCAL SETUP • You’ve all installed nodejs already, but haven’t used it for much • Simplest version: https://code.visualstudio.com/docs/nodejs/nodejs-tutorial • We’ll talk about how to run the bluemix example locally, though
  • 18. SET UP LOCAL SECURITY • Vcap-local.json • Go get it from the bluemix console and save it in the project • Make sure to add it to .gitignore!
  • 19. GITIGNORE • .gitignore is a file that tells git not to add certain files to your repository. Chief among these are any files that have security credentials. Since your git repo is public you don’t want these up there and for some credentials, like AWS keys, there are automated processes that scan public github repositories to steal them • Can also put other things you don’t need to save in there, for instance log files and the node_modules directory
  • 20. NPM • Npm install is the first thing you’ll have to do, that brings in all the dependencies • Then npm start
  • 21. ADDING ANOTHER ROUTE AND VIEW • Walk through the changes to app.js, adding an html page, and adding the route
  • 22. RUNNING LOCALLY AND DEBUGGING • We’ll cover actual debuggers later, but now there is a new place for console.log messages to go!
  • 23. EJS • EJS is a simple templating engine • It is used for things like headers and footers, but also to pass variables from node into your html • This way you can do data operations on the server side and just display on the client side • Good tutorial: https://scotch.io/tutorials/use-ejs-to-template-your-node-application • This is also a good resource: http://www.embeddedjs.com/getting_started.html
  • 24. PROJECT WORK • Possibilities: • Look through the EJS tutorial and familiarize yourself with it. You will want to understand this for next week. • Get a basic page up on bluemix and understand how to add routes, etc. • Work on putting other things up to your cloudant database – understand the JSON format and the calls to populate the database in app.js • Get your project 2 pages up on bluemix (although not necessarily hooked up yet, the pages themselves should be up there for this week’s homework.) As a stretch goal, break any headers and footers up into EJS partials and get them to display
  • 25. PROJECT 3 • You’ll need to implement a database backend for your project • Database should have new item, search, and delete (optionally edit) from the ui • You’ll need to implement a chatbot for your project • Is it intuitive to use? Does it do something useful? Does it have good coverage? • You’ll need to host your project on bluemix • You’ll need to show a working demo of your project 2 hooked up to these backend services
  • 26. PROJECT 3 GRADING • All project 3 presentation are due on November 29th in class, late projects will not be accepted • If you are horribly ill and nobody in your group can come in to present, the project itself is still due and the presentation marks will be forfeit, or if absolutely necessary you can send me a demo video • 15 points total • 4 points presentation • Is the demo good, is the website working, is the story about why it is interesting smooth • 4 points database design and function (do things actually work) • What is your table structure like? Talk about any problems you ran into. How was connecting it to the front end? • 3 points chatbot implementation – is it effectively designed and well implemented? • 4 points code quality and robustness • Not everything has to work, but if it is there I want it to do nothing at all or the right thing. How have you structured your code? Is it commented and easy to understand? Everything checked into github and deployed to the server? Good commit messages?
  • 27. HOMEWORK 9 (DUE NOV 8 BEFORE CLASS) • (can be done as a project group) • Take your project 2 website and port it up to Bluemix (welcome to make changes, but not required to) • Make sure that the github project is based off of the nodejs cloudant starter we discussed in class • Submit a link to the site running on Bluemix and a link to your github for the group • (to be done individually) • Get the site running locally on your machine so that you can efficiently make changes • Send me an email with a screenshot of the site running on your local machine