2-middleware
Middleware is just functions that take handlers and wrap them, producing
bigger handlers that do a little bit more. This example takes the handler from
1-hello and wraps it in one of the most useful
middlewares, the logger:
let () =
Dream.run
(Dream.logger (fun _ ->
Dream.respond "Good morning, world!"))
However, as you can see, the more middlewares we stack on top of each other
like this, the more parentheses and indentation we will end up with! To keep
the code tidy, we use @@, the
standard OCaml operator for calling functions without parentheses. So, the actual
code
in this example looks like this:
let () =
Dream.run
@@ Dream.logger
@@ fun _ -> Dream.respond "Good morning, world!"
When you run this server and visit http://localhost:8080, you get much more interesting (and colorful!) output:
You can write your own messages to the log using
Dream.log. See example
a-log for more logging options. Now that we have the
logger, we will use it in all other examples, even though it's not really
necessary — it just makes it much easier to see what is going on.
There's not much else to middlewares — they are really just functions
from handlers to handlers, so you can create them anywhere. Example
4-counter already shows a simple custom middleware.
Next steps:
- The next example,
3-router, shows routes, the other way to build up handlers in Dream. 4-counterbuilds the first custom middleware.
