#INCLUDE
Create For Community
Tech Cohort Workshop 5
INCLUDE
Agenda
Backend!
INCLUDE
INCLUDE
What is Backend?
Basically, handles all the logic of the app.
Key ideas: Servers, Databases, APIs, etc.
Core Idea
INCLUDE
What’s the point?
INCLUDE
INCLUDE
APIs
Application Programming Interface
INCLUDE
INCLUDE
API Endpoints
Eg, http://localhost:3000/api/contact-form,
https://pokeapi.co/api/v2/pokemon?offset=20&limit=30
INCLUDE
URL Query Parameters
INCLUDE
INCLUDE
Live Demo
INCLUDE
REST APIs
Representational State Transfer API
API that follows RESTful conventions: CRUD
Used across the web for data handling across many devices
INCLUDE
Create, Read, Update, Delete
POST (url) + payload/body (new data)
GET (url)
PUT (url) + payload/body (existing data identifier, new
data)
DELETE (url) + payload/body (existing data identifier)
INCLUDE
Core Idea: Create, Read, Update, Delete
Each operation consists of a conventional HTTP request method
INCLUDE
INCLUDE
RESTful Naming Conventions
Nouns not Verbs
INCLUDE
Pluralize, unless Singleton resources
INCLUDE
Use Hyphens to improve readability
INCLUDE
Use queries to filter/sort/paginate
INCLUDE
INCLUDE
Live Demo
INCLUDE
Fetch
JavaScript Fetch
INCLUDE
INCLUDE
Request & Response
Request & Response Objects
INCLUDE
Various Response Codes
Key Response Codes
200: Success
404: Not Found
500: Internal Server Error
…
Many more exist, but these are the most frequent
INCLUDE
INCLUDE
Synchronous vs. Asynchronous
Synchronous vs. Asynchronous
INCLUDE
Synchronous Programming
Asynchronous Programming
INCLUDE
Promises
JavaScript Promises
Think about how you might promise to pay your friend back after a meal.
Issues: Very verbose & can lead to long chains…
INCLUDE
Resolve & Reject
Functions that are built in parameters to a Promise Object
INCLUDE
Then & Catch Methods
INCLUDE
INCLUDE
Async & Await
JavaScript Async & Await
Alternative to JS Promises that offers cleaner & more readable code for handling API requests
INCLUDE
INCLUDE
Live Demo
INCLUDE
Error Handling
Using Try & Catch
INCLUDE
Asynchronous function with error handling
async function f() {
try {
let response = await fetch(‘http://someurl’)
const data = response.json()
return data;
} catch (err) {
return errorResponse; //with error msg
}
}
Throwing an Error
throw(error): Used to intentionally end a request
INCLUDE
INCLUDE
Creating your own API/server
External Server
Key Libraries for an External Server
express.js: Acts as our server
nodemon: Automatically reloads server on any changes
axios: Used as a safe alternative to JS built-in fetching
cors: Handles access perms to our server
INCLUDE
Next.js Serverless Functions
INCLUDE
INCLUDE
Postman
What is Postman?
3rd Party Application that allows us to interact with any external or personal server endpoints that we make.
INCLUDE
INCLUDE
Live Demo
INCLUDE
Thanks for Coming!
Text me on slack if yg need anything!