CRUD APIs & Express (Class 37-38) Flashcards

1
Q

What is internet?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When you type in url and enter, what happens?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is CRUD?

A

Create (post) - Make something
Read (get) - Get something
Update (put) - Change something
Delete (delete) - Remove something

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Using instagram as an example, what are some Create (post) requests?

A

Making a new post on instagram (I am sending data to a server and the server use the data to make a new post)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Using instagram as an example, what are some Read (get) requests?

A

Scrolling through your feed or looking at individual post of your friend (I am asking server to get me the data)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using instagram as an example, what are some Update (put) requests?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Using instagram as an example, what are some Delete (delete) requests?

A

Deleting a post

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In the browser, if i type instagram.com, what type of request is client making?

A

Get (Read) request

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When I like the photo on instagram, what kind of a request is client making?

A

Put (Update) request
and lines of code (API) in the server hears the request and respond back

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why requires 2 steps for liking the instagram post?

A
  1. Client send put request (Update) when liking a photo (to update data)
  2. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When I create a new post on instagram, what kind of a request is client making?

A
  1. Client sending Post (Create) request
  2. API in the server hears the request and respond by creating a new post for client
  3. When everything went ok, browser will refresh and send Read (Get) request and display a new post
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is Express?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is middleware?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is express (in simple terms)?

A

Web framework for node.js (can only be used with node.js)
(just like django for python, ruby on rails for Ruby, etc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Key steps before building

A

mkdir api-project
cd api-project
npm init
npm install express –save

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is npm init?

A

Create package.json file

17
Q

const express = require(“express”)
const app = express()

app.get(“/”,(request, response) => {

})

A

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.

18
Q

How to start a server?

A

node server.js

19
Q

What does server.js file usually contain?

A

Contains the code to create and listen on a network port (usually contains modules like “http” or express framework

20
Q

Common workflow

A
  1. Install Node.js:
    npm init in terminal
  2. Install Express
    npm install express
  3. Create server.js file
  4. Start the Server
    node server.js
21
Q
A