Lecture 5 - Backend Development
Lecture 5 - Backend Development
https://qz.com/1073221/the-hackers-who-broke-into-equifax-exploited-a-nine-year-old-security-flaw/
LaToza GMU SWE 432 Fall 2017 2
Today
• HW2 out, due next Tues before class
web server
Runs a program
Give me /myApplicationEndpoint
HTTP Response
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
<html><head>...
HTTP HTTP
HTTP HTTP Response
Response
Request Request (JSON)
(JSON)
HTTP HTTP
Request Request
HTTP HTTP
Microservice Response Microservice Response
Web Servers (JSON) (JSON)
Database
• Some advantages
• Reuse: use same micro service in multiple apps; use 3rd party
rather than first party services
• What’s wrong?
• How do you fix that?
ith
c t sw
e r a
int
d
en
ont
e fr
t h “Back End”
Wha t Data storage
Some
Firebase
other API Some other logic
th
wi
c ts
a
i ter
n“Back
d End”
nt
en Data storage
fr o
the Some other
at Firebase Some other logic
Wh API
Frontend Backend
Pros Pros
Very responsive (low latency) Easy to refactor between multiple
clients
Logic is hidden from users (good for
Cons security, compatibility, and intensive
Security computation)
Performance Cons
Unable to share between front-ends Interactions require a round-trip to
server
LaToza GMU SWE 432 Fall 2017 12
HTTP: HyperText Transfer Protocol
High-level protocol built on TCP/IP that defines how data is transferred
on the web
<html><head>...
const express
1: Make = require(‘express');
a directory, myapp
Import the module express
Create a callback for express to call when we have a “get” request to “/“. That
callback has access to the request (req) and response (res).
• PATH: string
“OK response”
Response status codes:
1xx Informational
2xx Success
3xx Redirection
4xx Client error
5xx Server error
“HTML returned
content”
Common MIME types:
application/json
application/pdf
image/png
LaToza
[HTML data] GMU SWE 432 Fall 2017 22
Response object
• Enables a response to client to be generated
• res.send() - send string content
• res.download() - prompts for a file download
• res.json() - sends a response w/ application/json
Content-Type header
• res.redirect() - sends a redirect response
• res.sendStatus() - sends only a status message
• res.sendFile() - sends the file at the specified path
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
LaToza GMU SWE 432 Fall 2017 24
Describing Errors
• 4xx Client Error: client did not make a valid request to server.
Examples:
• 400 Bad request (e.g., malformed syntax)
• 403 Forbidden: client lacks necessary permissions
• 404 Not found
• 405 Method Not Allowed: specified HTTP action not
allowed for resource
• 408 Request Timeout: server timed out waiting for a request
• 410 Gone: Resource has been intentionally removed and
will not return
• 429 Too Many Requests
<html><head>...
https://www.npmjs.com/package/node-fetch
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
• Intro to REST:
https://www.infoq.com/articles/rest-introduction