Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 2.79 KB

TODO.md

File metadata and controls

43 lines (33 loc) · 2.79 KB

TODO

Architecture

  • FinalHandler (see expressjs/finalhandler) Acts as the final middleware in the application. If no "next" handler is pased to handle, this is invoked when next() is called, and no more items remain in the stack. Will either return a 404 (no error) or a 500 (if an error was passed to next()).

  • Route (see connect's proto.js, in the use definition). Encapsulates a route and a handle, and is what the stack inside the app consists of.

    • Interestingly, the way "sub apps" (i.e., middleware handlers) are treated is that they get cast to a callable that calls their own handle method, and their "route" property is set to the given route. Not sure the latter is necessary, as it will be in the request.
  • Dispatcher (connect's call); accepts handle, route, err, req, res, and next.

    • Determines if arity of handler and whether or not an error is presen, and uses those to determine whether to call as an error handler or as a regular handler. If arity/error mismatch, nothing is called.
    • Does calling in a try/catch; if an error is caught, it is passed as the argument to next().
  • handle() defines done and next, and then calls next().

  • next() is a closure defined in the handle() logic. It pulls a handler off the stack; if none is present, it calls either the "next" (called out in connect) passed to handle, or the final handler. If a route is present on the handler, it checks to see if the current URL in the request matches. If not, it calls itself. If it does match, it munges the request URL to strip off the part matching, and also places the "removed" segment in the URL.

  • Potentially a Server. This would take an incoming request, pass it to the app/middleware, and likely be responsible for emitting the response at the end.

  • HTTP objects

    • Request
      • Ability to retrieve URL
      • Ability to retrieve method
      • Ability to retrieve headers
      • Ability to retrieve body
      • Ability to store arbitrary properties; retrieving unknown properties should return null values.
    • Response
      • Ability to set status code
      • Ability to set headers
      • Ability to set/append body
      • end() to indicate it should be sent
        • Only allow calling end() once

Handling

  • Matching the "route" of a handler is done in a case-insensitive manner. I.e., if you call use('/Api', handler), the application checks against /api.
  • Only match the handler route if it borders a /, ., or the end of the path.
  • Once matched, trim the matched part off, and store anything remaininginto the request URL, ensuring a leading slash. Store the original URL in an "originalUrl" property of the request (but only the very first time).