Skip to content

Instantly share code, notes, and snippets.

Last active November 7, 2022 11:00
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Cheat Sheet

Introduction to the Internet

By definition the Internet is a worldwide, publicly accessible series of interconnected computer networks that transmit data by packet switching using the standard Internet Protocol

  • Server Responses
    • 100: Informational
      • 100: Continue - Server receives request headers and client should proceed to send body
      • 101: Switching protocols
      • 102: Processing
    • 200: Success
    • 300: Redirection
    • 400: Client Error
    • 500: Server Error

HTTP: Hypertext transfer protocol

An application protocol for distributed, collaborative, hypermedia information systems. 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.

AJAX: Asynchronous Javascript and XML

    url: '',
    dataType: 'jason',
    // when successful
    succes: function(data, reqStr, jq) {
      console.log('data: ',data);
      console.log('string: '+reqStr); // don't need
      console.log('jqXHR: ',jq); // don't need
    // when there is an error
    error: function(jq, errStr) {
    // no matter what happens
    complete: function(jq, resStr) {
      console.log(jq) // jq is the jqXHR
      console.log(resStr) // resStr is whatever response string it gives you

APIs: Application program interfaces

Application program interface (API) is 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.

Node: Javascript runtime

  • Javascript runtime: virtual machine which interprets and executes JavaScript mostly on browser
  • Built by Google Chrome
  • asynchronous (ajax, click events, etc.)
    • code fires not necessarily in order, depending on events within the code
    • event driven
    • non-blocking
  • V8//Google engine
  Node                                      | Java/php/asp
    package manager                            code lives on server
    (npm) - web, ftp, smtp, etc.
    is its own server
    javascript everywhere
    Node environment has more
      features, don't need jQuery
  • sandbox - isolated environment

  • target the client's language, can change versions with nvm

    nvm ls-remote // shows all the versions available, can install different versions depending on client
  • create server 'node <file>' // executes javascript

NPM: Node's package ecosystem

Open source library

Express: Node framework

$ npm install express --save

EJS: embedded Javascript


<%      'Scriptlet' tag, for control-flow, no output
<%=     Outputs the value into the template (HTML escaped)
<%-     Outputs the unescaped value into the template
<%#     Comment tag, no execution, no output
<%%     Outputs a literal '<%'
%>      Plain ending tag
-%>     Trim-mode ('newline slurp') tag, trims following newline

Includes Includes are relative to the template with the include call. (This requires the 'filename' option.) For example if you have "./views/users.ejs" and "./views/user/show.ejs" you would use <%- include('user/show'); %>.

You'll likely want to use the raw output tag <%- with your include to avoid double-escaping the HTML output.

  <% users.forEach(function(user){ %>
    <%- include('user/show', {user: user}); %>
  <% }); %>


  • Middleware functions are functions that have access to

    • the request object (req)
    • the response object (res)
    • the next middleware function in the application’s request-response cycle
  • Middleware functions can perform the following tasks:

    • Execute any code.
    • Make changes to the request and the response objects.
    • End the request-response cycle.
    • Call the next middleware function in the stack.

MVC: Models, Views, Controllers

  • Process

    • request made by client
    • goes to Controller (controls routing logic)
    • from Controller to Model (handles database)
    • from Model back to Controller to Views (format data)
    • from Views, response to client
  • Components

    • The model directly manages the data, logic and rules of the application.
    • Views format the data for the client (ejs files)
    • The third part, the controller, accepts input and converts it to commands for the model or view.
  • Interactions

    • A model stores data that is retrieved according to commands from the controller and displayed in the view.
    • A view generates new output to the user based on changes in the model.
    • A controller can send commands to the model to update the model's state

MongoDB: Mongo Database

Classified as a NoSQL database MongoDB avoids the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.

REST: Representational State Transfer

  VERBS (request methods) - RESTful architecture
    - GET     : requests rep of specified resource
    - HEAD    : just give me the header
    - POST    : create new - forms (adds data to the end of the URL ?user_name=""&user_mail="" etc.)
    - PUT     : modify
    - DELETE  : deletes

Restful Routing : Client side

CRUD: Create, Read, Update, Delete

  verb        http verb       example of Rest Route
  Create      POST (html)     '/movies'                   --> create a new resource (movie) & return new URL (/movies/35)
                              (don't allow POST at ID ex. '/movies/ID')

  Read        GET  (html)     '/movies'                   --> show list of resources (movies) & return URLs (/movies/35, /movies/38, etc.)
                              ('/movies/:id'              --> show one resource (movie with id: 'id'))

  Update      PUT             '/movies/:id'               --> check for id, validate data, save result, replaces resource of id: 'id' & return /movies/:id or list of movies
                              (don't allow PUT for entire collection ex. '/movies')

  Delete      DELETE          '/movies' OR '/movies/:id'  --> check for internal id in DB (**check for dependencies), delete resource at :id & returns status code (204?)

HTML Forms

forms are only submitted if they have a name for each input field

  <form action = '/movies' method='get'>
    <input type='text' name='title' />

creates '/movies?title=blah&submit=true' ---> req.query

  <form action = '/movies' method='post'>
    <input name='title' />

creates '/movies' body = { title : 'blah', submit : 'true' } ---> req.body

Restful Routing : Server side


  app = express()

  app .get(filepath , function(req,res))
      .post( '' )
      .put( '' )
      .delete( '' )


    app .route('/movies')
        .put(fn1, fn2)


How to set up an Express app with middleware

In Node

npm install express
npm install morgan
npm install body-parser

In server.js: basic express app

  const express     = require('express');
  const app         = express();
  const server      = app.listen(3000);
  // Require middleware
  const logger      = require('morgan');
  const bodyParser  = require('body-parser');
  // use middleware

When using any middleware, don't forget next() or res.end at end of function

Using MVC architecture

mkdir Models Views Controllers

  • Models

    • module.exports data
  • Views

    • ejs files
    • linked in controller
  • Controllers

    • control where data goes, links to model and views

Seeding and connecting a MongoDB

  • Terminal

      mongod  --> starts server
      mongo   --> starts mongo shell, create databases, collections, and modify
      Start mongo >>        mongo`
      help                  help`
      show databases        show dbs
      use databases         use test
      add to databases      db.people.insert({name:"Celeste",age:27})
      find data             db.people.find({age:{ $gt: 30 }})
      sort data by stuff    db.people.find({age:{ $gt: 30 }}).sort({name:-1})
      update data           db.people.update({name:'Celeste'},{$set: {age:30}}, {multi:true})
      incriment data(+/-)   db.people.update({name:'Celeste'},{$inc:{age:1}}, {multi:true})
      remove data           db.people.remove({age:{$gt: 30}})
      drop json file to db  mongoimport --db test --collection restaurants --drop --file restaurants.json --jsonArray
  • Linking database to app in model or service

      const {MongoClient} = require('mongodb');
      const dbConnection = 'mongodb://localhost:27017/transit';
      module.exports = {
        list(req,res,next) {
        MongoClient.connect(dbConnection, function(err, db){
          if(err) throw err;
            .sort( {key:1} )
            .toArray( function(err, data){
              if(err) throw err;
              // get the data back
              res.lines = data;

Getting data from an API with AJAX

  • In public js file, use AJAX to access API with URL

Accessing data from an internal route with AJAX

  • Set up page that responds with json array of data, access that url with $.ajax

      let inputID = $('.id').val();
      let inputYear = $('.y').val();
      let inputDirector = $('.d').val();
      let inputTitle = $('.t').val();
    let queryObj = {};
      if(inputID) = inputID
      if(inputTitle) queryObj.t = inputTitle
      if(inputYear) queryObj.y = inputYear
      if(inputDirector) queryObj.d = inputDirector
      //we are requesting data from the /movies page to use in our ajax success
        url: '/movies',
        method: 'GET',
        dataType: 'json',
        data: queryObj,
        success: function(data){
          data.forEach(function(movie) {
            let $liTitle = $('<li>').text(movie.Title);
            let $img = $('<img>');
              $img.attr('src', movie.Poster)
            let $liYear = $('<li>').text(movie.Year);
            let $liDirector = $('<li>').text(movie.Director);
            let $liID = $('<li>').text(movie.imdbID);
            let $underline = $('<hr>');

Filtering data

  • Filter regular json file

          return == req.query.val;
  • Filter with Mongo DB

      // es6
      //const {MongoClient}     =require('mongodb').MongoClient
      const MongoClient = require('mongodb').MongoClient
          //when I go to this address, mongo will be there and we can speak
      const dbConnection = 'mongodb://localhost:27017/test' //location our database is being tested but only get the test database
          //export this whole object to be used over there
      module.exports = {
          //es6  - method for this object: searchMovies(){}
          //go and search movies
          searchMovies: function(req, res, next) {
              //empty object to fill with if statements below then use to pass into the .find
               //make sure capitalization matches the key from the object!
              const filterObj = {};
                //check for request queries t/y/d/id, if they are there set the
                //not if/elses, just ifs, special javascript word in
              if('id' in req.query) {
                //if it is then:
                filterObj['imdbID'] =
              if('y' in req.query) {
                //if it is then:
                filterObj['Year'] = req.query.y
              if('t' in req.query) {
                //if it is then:
                //the RegExp allows you to search through part of a string, i is for case insensitivity
                filterObj['Title'] = new RegExp( '^' + req.query.t, 'i')
              if('d' in req.query) {
                filterObj['Director'] = new RegExp( '^' + req.query.d, 'i')
              //mongo go to this database in this place and when you do run this function
              MongoClient.connect(dbConnection, function(err, db) {
                  if (err) throw err
                      //same as saying db.cruiseMovies - we're searching though our database test for a collection called cruiseMovies
                  //.find is a mongo property that requires an object
                  .find(filterObj)//filter from above
                  .sort({Year: -1, Title: 1})
                      //as the array is created do this:
                  .toArray(function(err, results) {
                          if (err) throw err
                              /* Act on the event */
                              //when i make the array i'm going to take the results and stick them onto the response
                          res.filteredMovies = results
                              //now fire the next function in the middleware chain for the server.js get request


  • app.js

    'use strict'
    const express         = require('express')
    const logger          = require('morgan')
    const path            = require('path')
    const bodyParser      = require('body-parser')
    const session         = require('express-session')
    const methodOverrise  = require('method-override')
    const homeRoute       = require('./routes/home')
    const userRoute       = require('./routes/user')
    const app             = express()
    const PORT            = process.env.PORT || process.argv[2] || 3000
    // set up session
      saveUnintialized: true,
      resave: true,
      secret: 'sooopersekret',
      cookie: {maxAge: 60000}
    // set up method override
    // set up logging so that we can see what's happening
    app.use('/bower_components', express.static(path.join(__dirname,'bower_components')));
    // set up ejs to render the views
    app.set('view engine', 'ejs')
    /* ROUTES */
    app.use('/', homeRoute)
    app.use('/user', userRoute)
    // set up server
    app.listen(PORT, function(){
      console.log("server up and running on port ", PORT)
  • views/index.ejs

    <!DOCTYPE html>
      <% include ./partials/head %>
      <% include ./partials/header %>
        <% if(user){ %>
          <%= user.fname %> <%= user.lname %>
        <% } else { %>
        <% } %>
  • views/user/new.ejs

    <!DOCTYPE html>
      <% include ../partials/head %>
      <% include ../partials/header %>
      <h1>New User</h1>
      <form action="/user/new" method="post">
        <label for="">First Name:</label>
        <input type="text" name="fname">
        <label for="">Last Name:</label>
        <input type="text" name="lname">
        <label for="">e-mail:</label>
        <input type="text" name="email">
        <label for="">Password:</label>
        <input type="password" name="password">
        <input type="submit" value="Create New User">
  • views/user/login.ejs

    <!DOCTYPE html>
      <% include ../partials/head %>
      <% include ../partials/header %>
      <form action="/user/login" method="post">
        <label for="">Email:</label>
        <input type="text" name="email">
        <label for="">Password:</label>
        <input type="password" name="password">
        <input type="submit" value="Login">
  • routes/home.js

    const homeRouter = require('express').Router();
    homeRouter.get('/', function(req,res){
      res.render('index', { user : req.session.user });
    module.exports = homeRouter;
  • routes/user.js

    const userRouter                  = require('express').Router()
    const { createUser, loginUser }   = require('../models/user')
    // means const createUser = require('../models/user').createUser
    userRouter.get('/new', function(req,res){
      res.render('user/new', { user : req.session.user });
    })'/new', createUser, loginUser, function(req,res){
      req.session.user = res.user;
        if(err) throw err;
    /* LOGIN */
    userRouter.get('/login', function(req,res){
      res.render('user/login', { user : req.session.user });
    })'/login', loginUser, function(req,res){
      req.session.user = res.user;
        if(err) throw err;
    /* LOGOUT */
    userRouter.get('/logout', function(req,res){
    module.exports = userRouter
  • models/user.js

    const { MongoClient }   = require('mongodb')
    const dbConnection      = 'mongodb://localhost:27017/auth_practice'
    const bcrypt            = require('bcrypt')
    const salt              = bcrypt.genSalt(10);
    function createSecure(email, password, callback) {
      bcrypt.genSalt(function(err, salt){
        bcrypt.hash(password, salt, function(err,hash){
          console.log('hash:', hash)
          callback(email, hash)
    function createUser(req,res,next){
      createSecure(, req.body.password, saveUser)
      function saveUser(email, hash){
        // posts to collection
        MongoClient.connect(dbConnection, function(err, db){
          if(err) throw err;
          let userInfo = {
            fname: req.body.fname,
            lname: req.body.lname,
            email: email,
            passwordDigest: hash
            .insertOne(userInfo, function(err, result){
              if(err) throw err;
    function loginUser(req,res,next){
      let email     =;
      let password  = req.body.password;
      MongoClient.connect(dbConnection, function(err,db){
        if(err) throw err;
          .findOne( { "email" : email }, function(err, user){
            if(err) throw err;
            if(user === null) {
              console.log("no user under e-mail", email);
            } else if(bcrypt.compareSync(password, user.passwordDigest)) {
              res.user = user;
            } else {
              console.log("password wasn't right")
    module.exports = { createUser, loginUser }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment