CRUD APIs & Express (Class 37-38) Flashcards
What is internet?
A wire connecting client side and server side.
Browser running on client side sends a request to server, server heard our request (through lines of codes, known as API) and respond back
When you type in url and enter, what happens?
You are making a get request to a server, and we can code our server to hear that request and to know what to do when it hears it.
What is CRUD?
Create (post) - Make something
Read (get) - Get something
Update (put) - Change something
Delete (delete) - Remove something
Using instagram as an example, what are some Create (post) requests?
Making a new post on instagram (I am sending data to a server and the server use the data to make a new post)
Using instagram as an example, what are some Read (get) requests?
Scrolling through your feed or looking at individual post of your friend (I am asking server to get me the data)
Using instagram as an example, what are some Update (put) requests?
Editing a comment or click the like (or love) icons (If my like goes from 0 to 1, I am updating something on the server)
Using instagram as an example, what are some Delete (delete) requests?
Deleting a post
In the browser, if i type instagram.com, what type of request is client making?
Get (Read) request
When I like the photo on instagram, what kind of a request is client making?
Put (Update) request
and lines of code (API) in the server hears the request and respond back
Why requires 2 steps for liking the instagram post?
- Client send put request (Update) when liking a photo (to update data)
- Server updated the data and refresh the page (refreshing trigger a new GET request and send new updated HTML)
Same is true with delete request
When I create a new post on instagram, what kind of a request is client making?
- Client sending Post (Create) request
- API in the server hears the request and respond by creating a new post for client
- When everything went ok, browser will refresh and send Read (Get) request and display a new post
What is Express?
Fast, un-opinionated, minimalist web framework for Node.js.
With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy
What is middleware?
A lot of stuff happens in between a request coming in and response going out. All the tools and things that help the process between the request and response
What is express (in simple terms)?
Web framework for node.js (can only be used with node.js)
(just like django for python, ruby on rails for Ruby, etc)
Key steps before building
mkdir api-project
cd api-project
npm init
npm install express –save
what is npm init?
Create package.json file
const express = require(“express”)
const app = express()
app.get(“/”,(request, response) => {
})
const express: This declares a constant variable named express. Constants are variables whose values cannot be re-assigned. In this case, express is the name of a Node.js module used for creating web applications.
require(“express”): This is a Node.js function used to import modules. Here, it imports the express module, which is a popular framework for building web applications in Node.js.
javascript
Copy code
const app = express();
const app: This declares another constant variable named app. This variable will hold our Express application.
express(): This invokes the express function, creating a new instance of an Express application. This instance represents your entire web application.
javascript
Copy code
app.get(“/”,(request, response) => {
});
app.get(“/”): This sets up a route handler for HTTP GET requests to the root URL (“/”) of your application. It tells Express to execute the provided callback function when a GET request is made to the root URL.
(request, response) => { … }: This is an arrow function, which is the callback function that will be executed when a GET request is made to the root URL. It takes two parameters: request (an object representing the HTTP request) and response (an object representing the HTTP response). Inside this function, you define the logic to handle the request and send a response back to the client.
Overall, these lines of code initialize an Express application, set up a route handler for the root URL (“/”), and define the logic to handle GET requests to that route.
How to start a server?
node server.js
What does server.js file usually contain?
Contains the code to create and listen on a network port (usually contains modules like “http” or express framework
Common workflow
- Install Node.js:
npm init in terminal - Install Express
npm install express - Create server.js file
- Start the Server
node server.js