Class 6 - Alwynblake/seattle-javascript-401n12 GitHub Wiki

#class 6 ###HTTP stands for Hypertext Transfer Protocol. It's a stateless, application-layer protocol for communicating between distributed systems, and is the foundation of the modern web.HTTP allows for communication between a variety of hosts and clients, and supports a mixture of network configurations. It assumes very little about a particular system, and does not keep state between different message exchanges.This makes HTTP a stateless protocol. 

###Communication between a host and a client occurs, via a request/response pair.

###The web communications are sent via URLs (uniform Resource Locations). URLs have a simple structure: protocol;//host:port/path/to/resource?query

###URLs reveal the identity of the particular host with which we want to communicate, but the action that should be performed on the host is specified via HTTP verbs. These are the most popular— they may be packaged as POST requests with the payload containing the exact action: create, update or delete.

  • GET: fetch an existing resource. The URL contains all the necessary information the server needs to locate and return the resource.
  • POST: create a new resource. POST requests usually carry a payload that specifies the data for the new resource.
  • PUT: update an existing resource. The payload may contain the updated data for the resource.
  • DELETE: delete an existing resource.

##E.g — in the server.js module:

*// Create task shell — application route *// Get url

  • App.get (‘/add’, showform); *// Post to the database
  • App.post(‘/add’, addTask);

*Function showForm (request, response) {

  • response.render (‘pages/add-view’);

*Function addTask (request, response) { *Let (title, description, category, contact, status} = request.body; *Let SQL = 'INSERT INTO tasks (title, description, category, contact, status) VALUES ($1,$2,$3,$4,$5);'; *Let values = [title, description, category, contact, status]; // Plug it into SQL *return client.query (SQL,values) *.then(reponse.edirect('/')) *.catch(err=>console.error(err)); }