Webprog

Cards (48)

  • Objects
    Used for named properties that give multiple values
  • Creating an empty object
    var myCar{}; or var myCar = new Object();
  • Using dot syntax
    myCar.make = "Toyota";
  • Using bracket notation

    let myCar = {}; myCar.make = "Ford"; myCar["make"] = "Ford";
  • Creating complex objects
    let props = ["make", "model"]; let vals = ["Audi", "A6"]; for (let i=0; i<props.length; i++){anotherCar[props[i]] = vals[i];}
  • Objects vs Arrays
    Both hold multiple pieces of data. Arrays hold a collection of things. Arrays with indexes dont benefit of being descriptive.
  • Complex objects
    Can be made up of scalar properties such as strings, integers and booleans. Arrays can be made up of objects, other arrays and scalar values.
  • JSON vs JavaScript Objects
    JSON: Properties/keys must be string written double quotes. JSON: Can have string, numbers, JSON object, array, null and boolean. JavaScript Objects: Not written with double quotes. JavaScript Objects: Can contains javascript values/types such as functions.
  • Using square bracket notation
    let student = { id: 119, firstName: "Nik", surname: "C-K" }; console.log(student["surname"] + ", " + student["firstName"]);
  • Traversing object properties

    for (let key in sydney){ console.log(key + "=" + sydney[key]); } if ("termNumber" in sydney){ console.log(sydney.termNumber); } else console.log("no term number");
  • Accessors/Mutators
    Use get and set as keywords. get total(){ return this.price * this.quantity; }
  • this keyword
    Access an object's members from inside an object definition
  • class keyword
    Uses prototypal inheritance
  • Using accessors/mutators
    get/set. Warning about setters: use different names for assigning variables and property names. get area(){ return Math.PI * this.radius ** 2; }
  • Object.keys()
    Accepts object and returns array of keys
  • Object.values()

    Accepts an object and returns array of values
  • Object.entries()
    Accepts an object, returns an array where each element is an array with 2 elements (key, value)
  • Bash is installed when installing git
  • node -v checks if node is installed
  • npm -v checks if node package manager is installed
  • npm init
    Initializes package manager
  • node app.js
    Used to run command line applications
  • Installing packages
    Downloading pre-existing files
  • Purpose of packages
    Node.js allows to add additional libraries to app without code functionality
  • Using "express" library
    Simplifies starting a server and managing requests/responses
  • npm install express
    Downloads node_modules, makes an entry in the package.json file, creates node_modules if it doesn't exist, package.json has a record now, package runs dependency/library
  • Starting server apps

    const express = require('express') const app = express() app.listen(3000, () => { console.log('listening on port 3000') })
  • Routing requests
    Associates an incoming url with a function. Views folder is made for hbs/html like files.
  • Express routes

    app.get("/your-route", (req, res) => {}); app.post("/your-request", (req,res) => {});
  • Express responses
    res.send("text or HTML"); res.render("template-file", {variable:value1, variable2:value2});
  • Serving static files
    app.use(express.static("public"));
  • Getting information from URL
    req.params contains route parameters in the url. req.query contains query parameters in url query string. req.body retrieves client's request from app.post();
  • GET method
    Data sent through url using query. Limited amount can be sent. Everything is visible in the url, not good for confidential info. Used for retrieving data that don't change server's state. Good for sharing/bookmarking data visible in the url.
  • POST method
    Data is sent in the body of HTTP request. No limited amount of data that can be sent. Data is not visible in the url, good for confidential info. Submit forms/operations that changes server's state. Not good for sharing/bookmarking data since invisible in url.
  • Testing routes is done by typing in the url or submitting forms
  • MySQL databases
    Use tables to organize data
  • Primary Key
    Uniquely identifies a record in a table
  • INT(M)
    Integer data type, M is the max value
  • CHAR(M)
    Fixed string length, M represents the exact column length in chars
  • VARCHAR(M)
    Variable length string, M represents the maximum column length in characters