Middleware
Lesson 1 of 3
  1. 1
    Writing code is a creative process. Programmers will be quick to differ in opinion on whether the solution to a problem should be implemented in one way or another — citing tradeoffs in algor…
  2. 2
    Beyond labeling, good code will leverage the strength of its programming language to avoid performing the same tasks Take a look at the following code: const addFive = number => { const fiveAd…
  3. 3
    By now you may have noticed that our efforts to not repeat ourselves have resulted in us putting the same function call over and over throughout our code. Isn’t that somewhat contradictory? You wou…
  4. 4
    It seems like our middleware was successful — it logged out . But then the response stopped there! What happened? We mentioned that most of Express’s functionality is chaining middleware. Th…
  5. 5
    Recall the function signature of an Express middleware, i.e., (req, res, next). You might recognize this signature as being the very same that we’ve used for Express routes in the past. Well there’…
  6. 6
    Now that we’ve managed to refactor our duplicate code into middleware functions, we should be noticing that our code contains much less repetition than before. Unfortunately, we still have duplicat…
  7. 7
    We’ve experienced writing middleware that performs its function and hands off the request and response objects to the next function in the stack, but why exactly do we have to write next() at the e…
  8. 8
    We learned that app.use() takes a path parameter, but we never fully investigated what that path parameter could be. Let’s take another look at the Express documentation for app.use(): “argument
  9. 9
    Recall that middleware is just a function with a specific signature, namely (req, res, next). We have, for the most part, been using anonymous function definitions for this because our middleware h…
  10. 10
    Knowing how to write middleware, we should now feel inspired to solve all the problems that come at us by writing code. It’s encouraging to know how to fix an issue. If we find a solution we don’t …
  11. 11
    With software we’ve personally written, invocation is a simple process. We already know what the code does, what it expects, and may have some notion how things could go wrong. Losing this intuitio…
  12. 12
    Being able to use open-source middleware can certainly make our jobs as programmers a lot easier. Not only does it prevent us from having to write the same code every time we want to accomplish a c…
  13. 13
    We’re almost finished with our Code Quality Checklist, there’s just one last problem to fix! When an error is thrown somewhere in our code, we want to be able to communicate that there was a proble…
  14. 14
    While it’s good to know how to write error-handling middleware, it’s a natural curiosity that causes us to ask “isn’t error-handling a common task? Has someone written middleware that performs it f…
  15. 15
    We’ve accomplished a lot! We learned what middleware is and we’ve used it to write cleaner, readable, adaptable, and maintainable code. We’ve written functions that are context aware and can have o…

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo