Quiz
- What is the relationship between node.js and express
Node.js runs JavaScript code (like Python runs Python code). Express is a module written in JS, like Python modules are written in Python. One Python equivalent to Express is Flask.
- Can you go over the examples from the reading please?
We will!
- And I think I am still a little confused abut the port section, if Express is listening for requests, does that mean they all come from the same port? Wouldn't they be coming from multiple ports if many different people are trying to send a request?
Your browser on your laptop sends requests from a particular port on your laptop to a particular port on the server. We care about the latter. We run our code on a particular port and that's where the requests go.
- I'm still a little confused about ports. When can ports can only be used by one person at a time, and when can they be the same between different devices?
You can only have one process listening on a particular port on a particular machine. Think of the port as a numbered door/room in a building.
Since we are all using Tempest, we need to use different ports. If we were all using our own laptops, we could all use the same port, say 8000 or 3000.
- Express opens an infinite loop to listen for requests, so can it handle multiple requests at the same time? or is there a first come first serve order?
Yes, it can handle multiple concurrent requests, but they are started FCFS. They might take different amounts of time to complete.
- Since Express is basically an infinite loop running and ready to handle any web requests, are all websites that handle requests always listening? Where is their code run from?
Yes. Web servers are always listening.
I don't know what you mean by "where is their code run from". The code runs on the server machine.
- Also a side question but you say to "Make of it what [we] will" about the MVC, would we benefit from trying to understand the MVC?
MVC is an abstraction of certain software designs. I'm not sure additional abstractions help us here, but if you already are familiar with the abstraction, it might be helpful in understanding Express.
- What is the .get() method even doing? I don't know if I'm missing it but I feel like I can't find out from the reading.
It's setting up an association between a URL (endpoint) and a function to handle requests to that endpoint:
- The reading mentions the req in the app.get() function has information about the incoming request. What information does it contain about the request?
Things like parameterized endpoint values, form values (get or post; we'll talk about those next time), cookie values (after the break), the client URL, the time, etc.
- general question, would be great to go over parameterized endpoints!
Let's do that now. See next
-
I don't quite get parameterized endpoints and this function:
app.get('/capital/:country', (req, res) => { let country = req.params.country; let city = capitals[country]; let page = '<h1>Capital</h1>'; page += `<p>${city} is the capital of ${country}</p>`; res.send(page); });
Great question. Let's go line by line. The first line sets up a handler for this parameterized URL (endpoint). All the urls will look like
/capital/something
like/capital/France
or/capital/Spain
. The function will respond to such requests.From the request object, we extract the country name, like
France
.We look up the capital city from our little database, like
Paris
.We create a string that includes some HTML code
We concatenate on a template string, filling in the city and country name.
We send that response to the client browser.
- I'd like to see parameterized endpoints in action!
Let's do it now!
- For the extra features (middlewares), do they call one each other in a chain?
Yes
- I'm still confused about what Morgan is - does it just console.log helpful messages for us?
Yes, it's a nice way of logging that a request was completed, its status and such.
- The handler function and next()
The handler function is at the end of the chain and does the custom work for this request. Almost all of our work will be on handler functions.
We will learn some middleware that will make our lives easier (say, parsing file upload requests).
- Also, is res.send similar to return?"
Return
is part of the JS language and has special meaning for function calls.But at a very general level, they are similar, in that information going back to the client.
- How do I manage asynchronous operations within an Express handler function?
The handler function can be asynchronous. Since Express is not waiting for a return value, but for us to invoke
res.send()
or similar, it's all good. We will this very soon. Next week.