Start a Web Server With a 'Hello World' Endpoint using dream

Task

Web Programming / Running a Web Server / Start a Web Server With a 'Hello World' Endpoint

Opam Packages Used

  • dream Tested with version: 1.0.0~alpha8 — Used libraries: dream

Code

The server:

  • Handles any incoming request on port 8080
  • Logs requests
  • Responds with a static Hello world message
  • Returns 404 error for other routes

let () =
  Dream.run
  @@ Dream.logger
  @@ Dream.router [
    Dream.get "/" (fun _ -> Dream.html "Hello World!");
    Dream.any "/" (fun _ -> Dream.empty `Not_Found);
  ]

Discussion

This example uses Dream, a simple and type-safe web framework for OCaml.

Recipe not working? Comments not clear or out of date?

Open an issue or contribute to this recipe!

Other Recipes for this Task