Express Flashcards
How do you add express to your package dependencies?
‘Npm install express’ into the terminal while in the package directory
What Express application method starts the server and binds it to a network PORT?
The listen method, takes the port number and a cb function
Is a computer a server?
Well… sort of. It runs the program the provides a service, but the computer itself is not a server.
Regarding HTTP status messages, what are the general categories for 100, 200, 300, 400, and 500 response codes?
100s —> informational responses
200s —> successful responses
300s —> redirection messages
400s —> client error responses
500s —> server error responses
How do you mount a middleware with an Express application?
When calling the use method, pass in the mount path as a parameter before the cb function
Which objects does an Express application pass to your middleware to manage the request / response lifecycle of the server?
The request object (req) and the response object (res). They also have access to the next middleware function in the applications lifecycle (typically via next).
What is the difference between front end and back end code?
Front End: Downloaded onto the users computer (or phone) and then runs on their device. Makes the website look and function the way it does.
Back End: Lives in a centralized location that we control —> typically in the cloud (AWS). Servers receive requests and deliver responses. Typically responses are either data or file(s
The first request a browser makes is for a…
Index.html page
Within the index.html, there are lots of other requests (css, js, images, etc.)
Middleware is just a fancy name for a…
Function that does something between two other things — in this case request and response
What is a RESTful API?
They use a path to determine the specific resource, and a method to dictate the action on that resource
Paths pretty much always start with…
/api —> This helps distinguish the front end and back end routing
App.use is a ‘weak’ match — what does that mean?
Any requests that start with that path will fire send —> it’s not very concrete
Hence, we typically run app.use() on all paths —> for example checking an API key or including express.json( )
When we’re defining specific routes, we use other methods, like…
App.post, app.get, app.patch, etc.
Once we send a response, can we send another response?
Nope —> you can only send one response per request.
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
Application/json; charset=Utf-8
What is the significance of an HTTP request’s method?
It shows the intent of what the request should do.
How do you represent a parameter in the path?
Use ‘:parameter-name’
How do you access path parameters inside of its code block?
Use ‘req.params’ to get all parameters with :’parameter-name’ as the key and passed in data as the value
Can also use ‘req.params.’parameter-name’ to get a specific parameter
Status code 204 is for…
No content. Cannot send data after sending this status code.
The property name in the params object is determined by…
:’parameter-name’ in the path
What does the express.json( ) middleware do and when would you need it?
It parses incoming requests with JSON data —> it populates the parsed data on the request object if successful (as req.object). Useful when you need to parse data into an object.
If you see body:undefined in the console where your server is running, what’s the issue?
You probably forgot to add the jsonMiddleware —> app.use(express.json( ))
What creates req.body?
It’s created by the json Middleware, and it assigns any JSON data in the request into an object
If you add a colon to the request arguments, what is the result?
score:=72 —> sends a number (72)
What is the default status code?
200
What status code do we use for post and what method do we typically chain on?
201.json(data-to-send)
What does Express.static return?
A middleware function serveStatic —> serves files from the directory (path) passed into express.static
What is the local __dirname in a Node.js module?
The directory name of the current module.
What does the join( ) method of Node’s path module do?
It joins all given path segments together into a beautiful final path that is OS specific (YEET)
What loads into the browser first?
The index.html document —> then the browser makes additional requests from there
StaticMiddleware is typically one of the first on your server —> why?
Any files that need loaded on the page should come first!