Node Boot Camp
-
Upload
troy-miles -
Category
Software
-
view
283 -
download
1
Transcript of Node Boot Camp
![Page 1: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/1.jpg)
Node Boot CampCowork South Bay Torrance, CA
21+22 Oct 2017
![Page 2: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/2.jpg)
Troy Miles• Troy Miles
• Nearly 40 years of experience
• Programmer, speaker & author
• @therockncoder
• lynda.com Author
![Page 3: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/3.jpg)
Day One• Modern JS
• CLI and REPL
• Node Web Server
• File System / Streams
• Express
• Heroku
![Page 4: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/4.jpg)
Day Two• Authentication with Passport
• MongoDB and Mongoose
• Socket IO
• Unit Testing
• Best Practices
![Page 5: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/5.jpg)
Check Versionsapp command my version
git git —version 2.14.0
node.js node -v 8.6.0
npm npm —v 5.3.0
![Page 6: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/6.jpg)
Introduction
![Page 7: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/7.jpg)
Node.js
• Node.js® is a platform built on Chrome's V8 JavaScript runtime for easily building fast, scalable network applications.
• v8.7.0
• https://nodejs.org/
![Page 8: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/8.jpg)
npm
![Page 9: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/9.jpg)
npm• The package manager for JavaScript
• npm requires a file, package.json, in the app root
• requires node.js because it is written in JavaScript
• doesn’t officially stand for anything
• https://www.npmjs.com/
![Page 10: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/10.jpg)
package.json• name - what this app is called, for npm it must be
• version - the current version number
• description - what this package is all about
• author - who wrote it
• repository - where it lives
![Page 11: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/11.jpg)
package.json
• license - type of license granted to users of the package
• scripts - commands
• dependencies - packages for production
• devDependencies - packages for development
![Page 12: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/12.jpg)
version definitions• “^1.8.9"
• 1 - is the major number
• 8 - is the minor number
• 9 - is the patch number
• patch number is not required
![Page 13: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/13.jpg)
version symbols• "1.8.9" - must exactly match version
• ">1.8.9" - must be greater than version
• ">=1.8.9", "<1.8.9", "<=1.8.9"
• "^1.8.9"- allows changes to the minor & patch
• "~1.8.9" - allows changes to the patch
• "1.8.*" - a wild card which stands for any value here
![Page 14: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/14.jpg)
install• The install adds a package to your app
• npm install <package name> [—save | —save-dev]
• npm i <package name> [-d | -D]
• —save: saves info to dependencies
• —save-dev: saves info to devDependencies
![Page 15: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/15.jpg)
run-script• npm run-script <command> <args>
• or npm run <command> <args>
• to abort use control-c
• start and test commands don’t need “run”
• npm start OR npm test
![Page 16: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/16.jpg)
Modern JavaScript
![Page 17: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/17.jpg)
JavaScript
![Page 18: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/18.jpg)
ECMAScript VersionsVersion Date
ES1 June 1997
ES2 June 1998
ES3 December 1999
ES4 DOA 2006
ES5 December 2009
ES2015 / ES6 June 2015
ES2016 / ES7 2016
![Page 19: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/19.jpg)
Collection Operators• .isArray()
• .every()
• .forEach()
• .indexOf()
• .lastIndexOf()
• .some()
• .map()
• .reduce()
• .filter()
![Page 20: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/20.jpg)
maplet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];console.log(nums);// map iterates over all of the elements and returns a new array with the same // number of elementslet nums2 = nums.map((elem) => elem * 2);console.log(nums2); /// [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40]
![Page 21: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/21.jpg)
filterlet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];console.log(nums);// filter iterates over the array and returns a new array with only the elements // that pass the testlet nums3 = nums.filter((elem) => !!(elem % 2));console.log(nums3); /// [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
![Page 22: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/22.jpg)
reducelet junk = [1, 2, 3, 4, 'Alpha', 5, {name: 'Jason'}];let letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];console.log(nums);// reduce iterates over the array passing the previous value and the current// element it is up to you what the reduction does, let's concatenate the stringslet letters2 = letters.reduce((previous, current) => previous + current);console.log(letters2); /// ABCDEFGHIJK// reduceRight does the same but goes from right to leftlet letters3 = letters.reduceRight((previous, current) => previous + current);console.log(letters3); /// KJIHGFEDCBA
![Page 23: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/23.jpg)
let
• let allows us to create a block scoped variables
• they live and die within their curly braces
• var is considered deprecated
• best practice is to use let instead of var
![Page 24: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/24.jpg)
let// let allows us to create block scoped variables// they live and die within the curly braceslet val = 2;console.info(`val = ${val}`);{ let val = 59; console.info(`val = ${val}`);} console.info(`val = ${val}`);
![Page 25: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/25.jpg)
const
• const creates a variable that can't be changed
• best practice is to make any variable that should not change a constant
• does not apply to object properties or array elements
![Page 26: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/26.jpg)
constconst name = 'Troy';console.info(`My name is ${name}`);// the line below triggers a type errorname = 'Miles';
![Page 27: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/27.jpg)
Template strings
• Defined by using opening & closing back ticks
• Templates defined by ${JavaScript value}
• The value can be any simple JavaScript expression
• Allows multi-line strings (return is pass thru)
![Page 28: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/28.jpg)
Template strings let state = 'California'; let city = 'Long Beach'; console.info(`This weekend's workshop is in ${city}, ${state}.`); // template strings can run simple expressions like addition let cup_coffee = 4.5; let cup_tea = 2.5; console.info(`coffee: $${cup_coffee} + tea: $${cup_tea} = $${cup_coffee + cup_tea}.`); // they can allow us to create multi-line strings console.info(`This is line #1.this is line #2.`);
![Page 29: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/29.jpg)
Arrow functions
• Succinct syntax
• Doesn’t bind its own this, arguments, or super
• Facilitate a more functional style of coding
• Can’t be used as constructors
![Page 30: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/30.jpg)
Arrow functions
• When only one parameter, parenthesis optional
• When zero or more than one parameter, parenthesis required
![Page 31: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/31.jpg)
Arrow functionlet anon_func = function (num1, num2) { return num1 + num2;}; console.info(`Anonymous func: ${anon_func(1, 2)}`);let arrow_func = (num1, num2) => num1 + num2;console.info(`Arrow func: ${arrow_func(3, 4)}`);
![Page 32: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/32.jpg)
this
• this is handled different in arrow functions
• In anonymous function this is bound to the global object
• In arrow function this is what it was in the outer scope
![Page 33: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/33.jpg)
Destructuring
• Maps the data on the right side of the equals sign to the variables on the left
• The data type decides the way values are mapped
• It is either object or array destructuring
![Page 34: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/34.jpg)
Object Destructuring16// this is a demo of the power of destructuring 17// we have two objects with the same 3 properties 18 const binary = {kb: 1024, mb: 1048576, gb: 1073741824}; 19 const digital = {kb: 1000, mb: 1000000, gb: 1000000000}; 20// We use a ternary statement to choose which object 21// assign properties based on their property names 22 const {kb, mb, gb} = (useBinary) ? binary : digital;
![Page 35: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/35.jpg)
Array Destructuring5 6 let [param1, bob, key] = ['first', 'second', '3rd']; 7 console.info(`param1 = ${param1}, bob = ${bob}, key = ${key}`); 8 // param1 = first, bob = second, key = 3rd
![Page 36: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/36.jpg)
Spread syntax
• Expands an expression in places where multiple arguments, elements, or variables are expected
![Page 37: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/37.jpg)
The spread operator11 12 // the spread operator 13 const myArray = ['Bob', 'Sue', 'Fido']; 14 function printFamily(person1, person2, pet) { 15 console.info(`Person 1: ${person1}, Person 2: ${person2}, and their pet: ${pet}`); 16 } 17 printFamily(...myArray); 18 // Person 1: Bob, Person 2: Sue, and their pet: Fido 19
![Page 38: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/38.jpg)
Class• Syntactic sugar over JavaScript use of function
constructors
• JavaScript uses proto-typical inheritance
• If a class extends another, it must include super() as first instruction in its constructor
• Only create a constructor if it does something
![Page 39: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/39.jpg)
Class• Syntactic sugar over JavaScript use of function
constructors
• JavaScript uses proto-typical inheritance
• If a class extends another, it must include super() as first instruction in its constructor
• Only create a constructor if it does something
![Page 40: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/40.jpg)
CLI and REPL
![Page 41: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/41.jpg)
CLI Purpose Short Long
version -v —version
help -h —help
evaluate script -e —eval
syntax check -c —check
require -r —require
![Page 42: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/42.jpg)
HTTP & Web Servers
![Page 43: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/43.jpg)
Using Environment Vars
• process.env object holds environment vars
• Reading: var dbConnect = process.env.dbConnect;
• Writing: process.env.mode = ‘test’;
![Page 44: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/44.jpg)
Encodings
• ascii - The ASCII character set
• utf8 - variable width, can represent Unicode chars.
• base64 - represents binary data in ASCII strings
![Page 45: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/45.jpg)
Callback Pattern4 // the Callback Pattern calls a user function once the operation completes 5 // the first value is a potential error 6 fs.readFile('../assets/declaration.txt', (err, content) => { 7 if (err) { 8 console.error(err); 9 throw err; 10 } 11 console.log(`file content`, content.toString()); 12 });
![Page 46: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/46.jpg)
Event Emitter Pattern3 // the Event Emitter Pattern - consist of two main parts 4 // the event emitter which in this case is the server 5 // and one ore more event listener 6 // here we have two listeners 'data' and 'end' 7 http.createServer((request, response) => { 8 if (request.method === 'POST' && request.url === '/echo') { 9 let body = []; 10 request.on('data', (chunk) => { 11 body.push(chunk); 12 }).on('end', () => { 13 body = Buffer.concat(body).toString(); 14 response.end(body); 15 }); 16 } else { 17 response.statusCode = 404; 18 response.end(); 19 } 20 }).listen(8080);
![Page 47: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/47.jpg)
File System
![Page 48: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/48.jpg)
Streams
![Page 49: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/49.jpg)
Express
![Page 50: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/50.jpg)
Express
![Page 51: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/51.jpg)
What is Express?
• Node.js is cumbersome
• Express simplifies it
• It sits in-between the user and Node.js
• For this reason it is called middleware
![Page 52: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/52.jpg)
Express & RESTful
• Express makes creating a RESTful API easy
![Page 53: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/53.jpg)
Heroku
![Page 54: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/54.jpg)
Heroku
![Page 55: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/55.jpg)
Heroku• Platform-as-a-Service (PaaS)
• Bought by Salesforce.com 2010
• Runs on top of AWS
• Supports Java, Node.js, Scala, Python, PHP, and Go
• heroku = heroic + haiku
![Page 56: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/56.jpg)
Installation• Create a free heroku account at:
• https://www.heroku.com
• Download + install heroku toolbelt at:
• https://toolbelt.heroku.com/
• (the account is free, no credit card necessary)
![Page 57: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/57.jpg)
Deploy to Heroku• heroku login
• heroku create <app-name>
• (must be unique)
• git push heroku master
• heroku open
![Page 58: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/58.jpg)
Authentication & Authorization
![Page 59: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/59.jpg)
RESTful API• Makes use of HTTP methodologies defined by RFC
2616 protocol
• Preferred over SOAP since it uses less bandwidth
• Breaks down a transaction into a series of HTTP methods
• Stateless by design
![Page 60: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/60.jpg)
GET MethodGET /resource
Request has body No
Successful response has body Yes
Safe Yes
Idempotent Yes
Cacheable Yes
![Page 61: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/61.jpg)
HEAD MethodHEAD /resource (HEAD * )
Request has body No
Successful response has body No
Safe Yes
Idempotent Yes
Cacheable Yes
![Page 62: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/62.jpg)
POST MethodPOST /resource
Request has body Yes
Successful response has body Yes
Safe No
Idempotent No
Cacheable No*
![Page 63: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/63.jpg)
PUT MethodPUT /new-resource
Request has body Yes
Successful response has body No
Safe No
Idempotent Yes
Cacheable No
![Page 64: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/64.jpg)
PATCH MethodPATCH /resource
Request has body Yes
Successful response has body No
Safe No
Idempotent No
Cacheable No
![Page 65: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/65.jpg)
DELETE MethodDELETE /resource
Request has body No
Successful response has body No
Safe No
Idempotent Yes
Cacheable No
![Page 66: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/66.jpg)
OPTIONS MethodOPTIONS /resource
Request has body No
Successful response has body Yes
Safe Yes
Idempotent No
Cacheable No
![Page 67: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/67.jpg)
MongoDB & Mongoose
![Page 68: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/68.jpg)
Top DB Engines1. Oracle
2. MySQL
3. MS SQL Server
4. PostgreSQL
5. MongoDB
6. DB2
7. MS Access
8. Cassandra
9. Redis
10.Elasticsearch
![Page 69: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/69.jpg)
Who Uses It?• Craigslist
• eBay
• Foursquare
• SourceForge
• Viacom
• Expedia
• Medtronic
• eHarmony
• CERN
• and more
![Page 70: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/70.jpg)
When to Use Mongo?• Document Database
• High Performance
• High Availability
• Easy Scalability
• Geospatial Data
![Page 71: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/71.jpg)
What is a Document?• An ordered set of keys and values
• Like JavaScript objects
• No duplicate keys allowed
• Type and case sensitive
• Field order is not important nor guaranteed
![Page 72: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/72.jpg)
A Contact Manager• first name
• last name
• home address
• work address
• mobile phone
• home phone
![Page 73: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/73.jpg)
In SQL
• Person table
• Address table
• Phone number table
• Joins necessary to retrieve complete record
![Page 74: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/74.jpg)
In Mongo
• One collection holds it all
• Including the arrays
• No joins necessary
![Page 75: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/75.jpg)
SQL to MongoDBSQL MongoDB
column field
row document
table collection
database database
joins none
transactions none
![Page 76: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/76.jpg)
MongoDB commands
• show dbs
• use <database name>
• show collections
• db.<collection name>.drop()
![Page 77: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/77.jpg)
CRUD
• Create: insert()
• Read: find()
• Update: update()
• Delete: remove(<query>)
![Page 78: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/78.jpg)
Queries• db.<collection name>.find(<query>)
• skip()
• limit()
• sort()
• pretty()
![Page 79: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/79.jpg)
mLab
• Database as a Service (DaaS) provider
• Supports AWS, Azure, App Engine
• Used by Fox, New York Times, Lyft, Toyota, SAP
![Page 80: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/80.jpg)
Sandbox Plan• mLab provides a free sandbox plan
• No credit card needed
• 0.5 GB memory limit
• Shared instance
• Forever free
![Page 81: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/81.jpg)
Socket I/O
![Page 82: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/82.jpg)
Unit Testing
![Page 83: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/83.jpg)
Jasmine
![Page 84: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/84.jpg)
Jasmine• Created by Pivotal Labs in 2010
• Current version 2.5.3
• JavaScript testing framework
• The default unit test for Angular
• Can test client and server code
![Page 85: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/85.jpg)
describe() - Test Suite• describe() is a global Jasmine function
• Takes to parameters
• name of the test suite (string)
• implementation of the suite (function)
• Can be nested
![Page 86: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/86.jpg)
describe()describe('App: Quizzer', () => { beforeEach(() => { TestBed.configureTestingModule({ declarations: [ AppComponent ], imports: [ RouterTestingModule ] }); }); });
![Page 87: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/87.jpg)
it() - Test Spec
• it() is a global Jasmine function
• Takes two parameters
• name of the spec (string)
• implementation of the spec (function)
![Page 88: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/88.jpg)
it()
it(`should have as title 'Quizzer'`, async(() => { let fixture = TestBed.createComponent(AppComponent); let app = fixture.debugElement.componentInstance; expect(app.title).toEqual('Quizzer'); }));
![Page 89: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/89.jpg)
expect() - Expectation• expect() is a global Jasmine function
• Jasmine’s version of assert()
• Takes one parameter
• The actual - value generated by code under test
• Is chained to a Matcher
![Page 90: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/90.jpg)
Matcher
• Takes the output of the expect() function
• Takes one parameter
• The expected value
• Compares the expect and actual value using the logic of the matcher
![Page 91: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/91.jpg)
expect()
let app = fixture.debugElement.componentInstance; expect(app).toBeTruthy(); expect(app).not.toBeUndefined(); expect(app.title).toEqual('Quizzer');
![Page 92: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/92.jpg)
Matchers (part 1)Matcher Comparison
toBe() compares using ===
toEqual() works for literal variables and objects
toMatch() for regular expressions
toBeDefined() compares against 'undefined'
toBeUndefined() also compares against ‘undefined'
![Page 93: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/93.jpg)
Matchers (part 2)Matcher Comparison
toBeNull() compares against null
toBeTruthy() truthy boolean casting
toBeFalsy() falsy boolean casting
toContain() finds an item in array
![Page 94: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/94.jpg)
Matchers (part 3)Matcher Comparison
toBeLessThan() mathematical comparison
toBeGreaterThan() mathematical comparison
toBeCloseTo() precision math comparison
toThrow() should throw an exception
![Page 95: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/95.jpg)
Custom Matchersvar customMatchers = { toBeGoofy: function (util, customEqualityTesters) { return { compare: function (actual, expected) { if (expected === undefined) { expected = ''; } var result = {}; result.pass = util.equals(actual.hyuk, "gawrsh" + expected, customEqualityTesters); result.message = result.pass ? "Expected " + actual + " not to be quite so goofy" : "Expected " + actual + " to be goofy, but it was not very goofy"; return result; } }; } };
![Page 96: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/96.jpg)
beforeEach()
• Setup function
• Called before each spec is executed
• A good place to add customer matchers
![Page 97: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/97.jpg)
beforeEach()
beforeEach(function() { jasmine.addMatchers(customMatchers); });
![Page 98: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/98.jpg)
this
• beforeEach sets the this construct to any empty object
• It is passed to each it() and afterEach()
• The modified this doesn’t flow thru from one it() to the next
![Page 99: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/99.jpg)
afterEach()
• Teardown function
• Called after each spec is executed
![Page 100: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/100.jpg)
Disabling suites & specs
• prepend an ‘x’
• to disable a suite change describe() to xdescribe()
• to disable a spec change it() to xit()
• They are not execute but appear in reporting
![Page 101: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/101.jpg)
Best Practices
![Page 102: Node Boot Camp](https://reader031.fdocuments.us/reader031/viewer/2022021500/5a66bc057f8b9a3c0e8b4dbb/html5/thumbnails/102.jpg)
Wrapping Up