Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static files OCaml example in readme #33

Closed
gtach2o opened this issue Feb 2, 2020 · 1 comment
Closed

Static files OCaml example in readme #33

gtach2o opened this issue Feb 2, 2020 · 1 comment

Comments

@gtach2o
Copy link

gtach2o commented Feb 2, 2020

There are some typos in readme for OCaml static files example, but I was able to make it work like this:

let server_config: unit Naboris.ServerConfig.t = Naboris.ServerConfig.create ()
  |> Naboris.ServerConfig.setRequestHandler(fun route req res ->
    match ((Naboris.Route.meth route), (Naboris.Route.path route)) with
      | (Naboris.Method.GET, "static" :: static_path) ->
        Naboris.Res.static (Sys.getcwd() ^ "/static") static_path req res 
      | (Naboris.Method.GET, "/" :: []) ->
        Naboris.Res.static (Sys.getcwd() ^ "/static") ["/index.html"] req res
      | (Naboris.Method.GET, "" :: []) ->
        Naboris.Res.static (Sys.getcwd() ^ "/static") ["/index.html"] req res
      | _ ->
        res
          |> Naboris.Res.status 404
          |> Naboris.Res.text req "Resource not found.";
  )

let _ = Lwt_main.run begin
    Naboris.listenAndWaitForever 3000 server_config
end

By this index.html file from static folder is opened when gethttp://localhost:3000/ or http://localhost:3000.
Would be nice to have static folder by default or via parameter in ServerConfig for serving up static files. It can be useful for SPA.

@shawn-mcginty
Copy link
Owner

Created issues to address this. Great find!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants