# Bonito: serving an HTML page

**URL:** <https://discourse.julialang.org/t/bonito-serving-an-html-page/123872>\
**Category:** Web Stack\
**Tags:** bonito\
**Created:** [December 16, 2024, 6:29am UTC](https://discourse.julialang.org/t/bonito-serving-an-html-page/123872 "2024-12-16T06:29:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 16, 2024, 6:29am UTC](https://discourse.julialang.org/t/bonito-serving-an-html-page/123872/1 "2024-12-16T06:29:21Z")

</div>

In Bonito.jl, how should I serve an HTML file? I would have expected

```julia
    server = Server("0.0.0.0", 8080)
    route!(server, "/" => title_page)
    route!(server, "/documentation" => HTMLPage("/local/path/to/my/page.html")

```

but looking at the docs I couldn’t find anything. I guess I can create an `App` which opens the file and returns its content inside an `HTML()` but that’s very roundabout.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [December 16, 2024, 11:02am UTC](https://discourse.julialang.org/t/bonito-serving-an-html-page/123872/2 "2024-12-16T11:02:02Z")

</div>

> [@cstjean](#):
>
> ```julia
> server = Server("0.0.0.0", 8080)
> route!(server, "/" => title_page)
> route!(server, "/documentation" => HTMLPage("/local/path/to/my/page.html")
> 
> ```

Hm, that hasn’t been a use case so far, so I guess your workaround is the best option.  
I guess one could implement `route!(server, "/" => Asset("../test.html"))`, by overloading:

```julia
function HTTPServer.apply_handler(app::Asset, context)
   server = context.application
   request = context.request
  ...
end

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 17, 2024, 7:37am UTC](https://discourse.julialang.org/t/bonito-serving-an-html-page/123872/3 "2024-12-17T07:37:24Z")

</div>

Thank you for the suggestion! I tried my hand at `HTML(read(my_file, String))`, but it’s turning out more complicated than I thought. I’m trying to serve Documenter.jl documentation output. I can serve the `.html` file I care about fine, but then its styling is all wrong, because I’m not also serving the Documenter.jl-generated files in `/docs/build/assets`. Maybe I could serve those as `Asset`? It’s a bit painful, but it looks like the simplest solution.

Otherwise, I guess I could set up an Apache Server to serve the `/docs/build` repository on a different port, and link to those pages. Or maybe go with Oxygen.jl? I don’t have any experience there, so any advice is appreciated.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 17, 2024, 8:00am UTC](https://discourse.julialang.org/t/bonito-serving-an-html-page/123872/4 "2024-12-17T08:00:26Z")

</div>

LiveServer.jl is kinda neat and solves my problem in the short term, with a `localhost` link. Not sure it’ll work out once I’m on a server, but it’ll be worth a try!

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [December 17, 2024, 10:05am UTC](https://discourse.julialang.org/t/bonito-serving-an-html-page/123872/5 "2024-12-17T10:05:14Z")

</div>

> Otherwise, I guess I could set up an Apache Server to serve the `/docs/build` repository

You should be able to also serve a whole folder with Bonito.  
Here is a prototype:

```julia
using Bonito

struct Folder
    path::String
end

function Bonito.HTTPServer.apply_handler(folder::Folder, context)
    target = context.request.target[2:end] # first should be '/' which we want to remove for a path
    # Bonito.html is just a simple helper to return a HTTP html response
    return Bonito.html(read(joinpath(folder.path, target)))
end

server = Server("0.0.0.0", 8080)
route!(server, r"/.*" => Folder(pwd()))

```

If you get it to work, a PR would be nice, since I guess others may want to have this functionality 😉
