Skip to content

Express Routing & Connected API

all1124 edited this page Oct 21, 2019 · 3 revisions

Express Routing & Connected API

  • Routing refers to how an application’s endpoints (URIs) respond to client requests
  • You define routing using methods of the Express app object that correspond to HTTP methods
  • The application “listens” for requests that match the specified route(s) and method(s), and when it detects a match, it calls the specified callback function
  • Route paths, in combination with a request method, define the endpoints at which requests can be made
    • Route paths can be strings, string patterns, or regular expressions
      • The characters ?, +, *, and () are subsets of their regular expression counterparts
      • The hyphen (-) and the dot (.) are interpreted literally by string-based paths
  • Router is like a mini express application
    • It doesn't bring in views or settings, but provides us with the routing APIs like .use, .get, .param, and route
  • Only need one dependency, Express
  • We can call an instance of the express.Router(), apply routes to it, and then tell our application to use those routes
  • router.use() to define middleware
    • This will now be applied to all of the requests that come into our application for this instance of Router
  • The order you place your middleware and routes is very important
    • Everything will happen in the order that they appear
    • This means that if you place your middleware after a route, then the route will happen before the middleware and the request will end there
    • Your middleware will not run at that point

Clone this wiki locally