Chevron Left Icon
Learn Express Routes
Lesson 1 of 3
Chevron Right Icon
  1. 1
    A huge portion of the Internet’s data travels over HTTP/HTTPS through request-response cycles between clients and servers . Every time that you use a website, your browser sends requests to one or…
  2. 2
    Express is a Node module, so in order to use it, we will need to import it into our program file. To create a server, the imported express function must be invoked. const express = require(‘expres…
  3. 3
    Once the Express server is listening, it can respond to any and all requests. But how does it know what to do with these requests? To tell our server how to deal with any given request, we register…
  4. 4
    HTTP follows a one request-one response cycle. Each client expects exactly one response per request, and each server should only send a single response back to the client per request. The client is…
  5. 5
    Express tries to match requests by route, meaning that if we send a request to : /api-endpoint, the Express server will search through any registered routes in order and try to match ‘/api-endpoin…
  6. 6
    Routes become much more powerful when they can be used dynamically. Express servers provide this functionality with named route parameters. Parameters are route path segments that begin with : in…
  7. 7
    Express allows us to set the status code on responses before they are sent. Response codes provide information to clients about how their requests were handled. Until now, we have been allowing t…
  8. 8
    Parameters are extremely helpful in making server routes dynamic and able to respond to different inputs. Route parameters will match anything in their specific part of the path, so a route matchin…
  9. 9
    HTTP Protocol defines a number of different method verbs with many use cases. So far, we have been using the GET request which is probably the most common of all. Every time your browser loads an…
  10. 10
    You may have noticed in the previous exercise that our PUT route had no information about how to update the specified expression, just the id of which expression to update. It turns out that there …
  11. 11
    Express matches routes using both path and HTTP method verb. In the diagram to the right, we see a request with a PUT verb and /expressions (remember that the query is not part of the route path). …
  12. 12
    POST is the HTTP method verb used for creating new resources. Because POST routes create new data, their paths do not end with a route parameter, but instead end with the type of resource to be cre…
  13. 13
    DELETE is the HTTP method verb used to delete resources. Because DELETE routes delete currently existing data, their paths should usually end with a route parameter to indicate which resource to de…
  14. 14
    Congratulations, you have made our glorious Express Yourself Machine fully operational! Not only that, you now have all the tools you need to create a basic CRUD API! Time to practice your skills…
  15. 15
    In this exercise, you were able to create a full server allowing users to implement all CRUD operations for two kinds of resources: Expressions and Animals! With these skills and knowledge of the H…
  1. 1
    Your Expressions/Animals routes are all working well, and our machine is fully functional! Our app.js file, however, is getting quite long and hard to read. It’s easy to imagine that as we add …
  2. 2
    An Express router provides a subset of Express methods. To create an instance of one, we invoke the .Router() method on the top-level Express import. To use a router, we mount it at a certain pa…
  3. 3
    Generally, we will keep each router in its own file, and require them in the main application. This allows us to keep our code clean and our files short. To do this with monstersRouter, we would c…
  4. 4
    As you saw in the previous exercise, when using routers, it’s important to remember that the full path of a request can be segmented. In the diagram to the left, you can create an Express applicat…
  5. 5
    Now that you’ve learned about nested route matching, let’s refactor the rest of the /expressions routes into expressions.js.
  6. 6
    Okay, training wheels off! Now refactor all your /animals routes to animals.js.
  7. 7
    Great job—you created a much more readable and clean code base using Express.Router(). Writing clean and readable code is one of the most important skills as a developer. This will make addin…
  1. 1
    Code Challenge
  2. 2
    Code Challenge
  3. 3
    Code Challenge
  4. 4
    Code Challenge
  5. 5
    Code Challenge
  6. 6
    Code Challenge
  7. 7
    Code Challenge
  8. 8
    Code Challenge
  9. 9
    Code Challenge
  10. 10
    Code Challenge

How you'll master it

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

Pro Logo