Skip to content

Instantly share code, notes, and snippets.

@heidipowers
Last active July 6, 2016 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heidipowers/05fd5b8d94e133ef696e88690a9adb0a to your computer and use it in GitHub Desktop.
Save heidipowers/05fd5b8d94e133ef696e88690a9adb0a to your computer and use it in GitHub Desktop.
It's a cheat-sheet

CHEATY SHEETY

Introduction To The Internet: It's Magic

HTTP: Hypertext Transfer Protocol

  • HTTP is the foundation of data communication for the World Wide Web.
  • Hypertext is structured text that uses logical links (hyperlinks) between nodes containing text.
    • http: protocol *www: subdomain *whatever(dot)com: domain and Top Level Domain (TDL)
    • ? ends URL (Universal Resource Locator) starts query string
    • #footer: client-side (any device) hash anchor

Error Handling:

* 100: Informational
* 200: Success
* 300: Redirection
* 400: Client Error
* 500: Server Error

Common Errors:

200 OK This response code indicates that the request was successful.

201 Created This indicates the request was successful and a resource was created. It is used to confirm success of a PUT or POST request.

400 Bad Request The request was malformed. This happens especially with POST and PUT requests, when the data does not pass validation, or is in the wrong format.

404 Not Found This response indicates that the required resource could not be found. This is generally returned to all requests which point to a URL with no corresponding resource.

401 Unauthorized This error indicates that you need to perform authentication before accessing the resource.

405 Method Not Allowed The HTTP method used is not supported for this resource.

409 Conflict This indicates a conflict. For instance, you are using a PUT request to create the same resource twice.

500 Internal Server Error When all else fails; generally, a 500 response is used when processing fails due to unanticipated circumstances on the server side, which causes the server to error out.

AJAX: Asynchronous Javascript & XML

  • Ajax is a client-side script that communicates to and from a server/database without the need for a postback or a complete page refresh.
  • The best definition I've read for Ajax is “the method of exchanging data with a server, and updating parts of a web page - without reloading the entire page.”
  • Ajax accesses data with a URL

Every Ajax request returns a readystate:

* 0: Request Not Initialized
* 1: Server Connection Established
* 2: Request Received
* 3: Processing Request
* 4: Request is finished & Response Is Ready

Ajax Methods (VERBS)

  • GET - I want a response, show me the data/collection (http READ)
  • POST - I want to create data (http CREATE)
  • PUT - I want to replace/update data/collection (http UPDATE)
  • DELETE - I want to delete data/a collection (http DELETE)

HTTP Mehtods (VERBS)

  • CREATE - (ajax POST)
  • READ - (ajax GET)
  • UPDATE - (ajax PUT)
  • DELETE - (ajax DELETE)

API: Application Programming Interface

  • Api's help you interface with other technology, usually for the purpsoe of accessing information or content from servers
  • Api's are a set of routines, protocols, and tools for building software applications. An API specifies how software components should interact and APIs are used when programming graphical user interface (GUI) components.
  • The API defines the correct way for a developer to write a program that requests services from an operating system (OS) or other application.
  • APIs are implemented by function calls composed of verbs and nouns.The required syntax is described in the documentation of the application being called.

REST/ReST: Representational State Transfer

  • REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.
  • Relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.
  • RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.

NODE

  • Servers speak in protocol and handle/respond to requests and allow for more complex applicatons.
  • Node.js is a platform built on Chrome's JavaScript runtime and Google's V8 engine for easily building fast and scalable network applications.
  • Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
  • Node has a package manager, NPM.
  • NPM makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that is being shared.
  • These bits of reusable code are called packages, or sometimes modules. A package is just a directory with one or more files in it, that also has a file called "package.json" with some meta data about this package.
  • A typical application, such as a website, will depend on dozens or hundreds of packages. These packages are often small. The general idea is that you create a small building block which solves one problem and solves it well. This makes it possible for you to compose larger, custom solutions out of these small, shared building blocks.
  • NPM Documenation: https://docs.npmjs.com/
  • Node Documentation: https://nodejs.org/api/

NPM Common Commands:

Node in terminal: node filename.js
npm init // setup wizard, must be in project directory
npm install --save dependency1 dependency2 dependency 3
npm unistall --save dependency1
nodemon: allows constant editing without restarting your server

NPM Packages:

MVC Architechture

  • Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces on computers.
  • It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.
    • Model: The lowest level of the pattern which is responsible for maintaining data.
    • View: This is responsible for displaying all or a portion of the data to the user.
    • Controller: Code that controls the interactions between the Model and View.
  • MVC is popular as it isolates the application logic from the user interface layer and supports separation of concerns. Here the Controller receives all requests for the application and then works with the Model to prepare any data needed by the View. The View then uses the data prepared by the Controller to generate a final presentable response.

MVC:

  • set up app.js - constants, filepaths for controllers, middleware, etc
  • Ajax searches app.js for filepath
  • controllers.js - set up router, export router, fun tion(s) that set the path to res/req
  • module.exports - make data in the file available somewhere else
  • URL is set to controller to get the path

Mongo

  • First Terminal: mongod
  • Second Terminal: mongo
    • show dbs
    • show collections
    • use dbName
    • mongoimport

CODE Snippets & Such

npm install --save express morgan path ejs body-parser
mongoimport --db NAME --collection NAME --drop --file path/to/file —jsonArray
run sublime snippet 'express' in a js file for app.js basic setup
= new RegExp( '^' + req.query.t, 'i') //space and case insensitive

const SortByYearThenTitle = function(a,b){
    return (a.Year-b.Year !== 0)? a.Year-b.Year
    : (a.Title == b.Title)? 0
    : (a.Title < b.Title) ? -1
    : 1
}

   //If a query parameter for state exists, use that. Otherwise, return everything
    const state = (req.query.s) ? req.query.s : sighting.state
    return sighting.state === state
    }

Files To Review

  • unit2/d09 homework = rats apps (jquery for rows/columns found here)
  • unit2/d09 classwork = cruise database
  • unit2/d08 instructor = candyshop
  • unit2/d08 homework = birds/sightings (ejs for dynamic rows/columns found here)
@heidipowers
Copy link
Author

SQL - Structured Query Language
Use SQL to query Entity/Model

  • Attributes are columns
  • Records are rows

ERD - Entity Relationship Diagram
map out a relationship - database should be well planned in advance

ID: Serial (sequence)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment