Start a Web Server That Serves an HTML Template using cohttp-lwt-unix

Task

Web Programming / Running a Web Server / Start a Web Server That Serves an HTML Template

Opam Packages Used

  • cohttp-lwt-unix Tested with version: 5.3.0 — Used libraries: cohttp-lwt-unix
  • cohttp Tested with version: 6.1.0 — Used libraries: cohttp
  • tyxml Tested with version: 4.6.0 — Used libraries: tyxml

Code

The server:

  • Handles any incoming request on port 8080
  • Responds with a static HTML template that displays "Hello World!"
  • Uses Server.respond_string to return the template with headers set as html/text
  • Uses Cohttp_lwt_unix for asynchronous operations
  • Uses Cohttp for HTTP handling

open Cohttp
open Cohttp_lwt_unix
open Tyxml.Html

let html_template =
  html
    (head (title (txt "Template Page")) [meta ~a:[a_charset "UTF-8"] ()])
    (body [h1 [txt "Hello World!"]])

let response_body = Format.asprintf "%a" (pp ()) html_template

let () =
  let callback _conn _req _body =
    Server.respond_string ~status:`OK ~body:response_body 
      ~headers:(Header.init_with "Content-Type" "text/html") ()
  in
  let server = Server.make ~callback () in
  let port = 8080 in
  let mode = `TCP (`Port port) in
  Format.printf "listening on http://localhost:%d\n%!" port;
  Server.create ~mode server |> Lwt_main.run

Discussion

This example demonstrates how to build an HTTP server using cohttp-lwt-unix and safely render an HTML template with Tyxml in OCaml

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

Open an issue or contribute to this recipe!

Other Recipes for this Task