Skip to content

Introduction to Middleware

Joshua Segal edited this page Mar 3, 2021 · 1 revision

Middleware in JServe (in the 1.0 release) are setup in an extremely simple way.

All middleware is loaded (before running the server), and whenever the server is at a specific point in the code (that is to say, dealing with a request, a response, etc), JServe will go through all event handlers that have been registered under that event.

Currently, there exist 3 events available to middleware event handlers:

  1. HTTP-REQUEST-RECEIVED
  2. HTTP-RESPONSE-READY
  3. HTTP-RESPONSE-SENT

The order in which these events are listed is the same order in which they are triggered.

HTTP-REQUEST-RECEIVED

This event is triggered when a request has been received by the server, but nothing has been done about it yet. The only thing that has happened is the conversion of the request into a data structure so event handlers can easily deal with the information in a request as best as possible (specifically, the RequestParser data structure)

HTTP-RESPONSE-READY

This event is triggered when a request has been processed and an appropriate response created (so after the HTTP-REQUEST-RECEIVED event and other server code) but the server has not sent the response, yet. This gives event handlers the opportunity to do anything else before responses are sent, like inserting content into responses (specifically, the ResponseStream data structure)

HTTP-REQUEST-SENT

This event is triggered when a request has been received (HTTP-REQUEST-RECEIVED), an appropriate response created (HTTP-RESPONSE-READY), and the same response sent to the client, at which point nothing can be done with the response (ResponseStream), but any last things that need to be done by the middleware should be done here.