Quiz

  1. Can we talk a bit more about express? I am still a bit confused with Express and middleware.

    Sure. You can think of Express as one of those old-fashioned telephone switchboard operators. A call comes in, and the operator does some routine stuff (greeting, announce the company, ask whom they want to talk to) and then routes the call. Our code (one of our handler functions), then takes over the call.

    To think about middleware, we have to imagine some other steps in that chain. Maybe the call is routed to an executive, who has a secretary/admin assistant, who does more processing before (possibly) handing the request off to the executive. Maybe the assistant can handle the request themselves.

  2. Just to clarify, Morgan is just a name, and it could be named as anything?

    Yes. Morgan is just the name of a small Node.js Express module that logs the final status of the request. I don't know why they called it that.

  3. Can we talk more about middleware? / Can we go over more examples on middleware?

    Sure, and we'll see more examples as the semester goes on. I wouldn't worry about it a lot at this point.

    One important one we will encounter in a few weeks parses requests that have attached files, so we can handle file upload.

  4. I’m still confused on how middleware works and where the next function comes from.

    The next function is determined by other things that we have added to the chain by app.use(middleware). Eventually, the end of the chain is the handler function that is associated with an endpoint. The endpoint is where 99% of our work will go, so that's what to focus on.

    We'll revisit middleware when it comes up. But for now, I suggest sticking to the switchboard metaphor.

  5. Are there other cool use cases for middlewares other than printing out logs?

    Yes, such as the file-parsing one, which is called multer. There's another one for handling cookies and another for handling sessions; we'll get to them soon.

    See using middleware and third-party middleware.