Serve index.html at / by default in Compojure

前端 未结 8 1888
无人及你
无人及你 2020-12-28 14:19

I have a static file called index.html that I\'d like to serve when someone requests /. Usually web servers do this by default, but Compojure doesn

相关标签:
8条回答
  • 2020-12-28 14:35

    This would be a pretty simple Ring middleware:

    (defn wrap-dir-index [handler]
      (fn [req]
        (handler
         (update-in req [:uri]
                    #(if (= "/" %) "/index.html" %)))))
    

    Just wrap your routes with this function, and requests for / get transformed into requests for /index.html before the rest of your code sees them.

    (def app (-> (routes (your-dynamic-routes)
                         (resources "/"))
                 (...other wrappers...)
                 (wrap-dir-index)))
    
    0 讨论(0)
  • 2020-12-28 14:38

    This works just fine. No need to write a ring middle-ware.

    (:require [clojure.java.io :as io])
    
    (defroutes app-routes 
    (GET "/" [] (io/resource "public/index.html")))
    
    0 讨论(0)
  • 2020-12-28 14:47

    when other code doesnt work, try this code.

    (GET "/about/" [] (ring.util.response/content-type
                         (ring.util.response/resource-response "about/index.html" {:root "public"}) "text/html"))
    
    0 讨论(0)
  • 2020-12-28 14:49
    (ns compj-test.core
      (:use [compojure.core])
      (:require
            [ring.util.response :as resp]))
    
    (defroutes main-routes
      (GET "/" [] (resp/redirect "/index.html")))
    

    What you're asking for is a redirect from / to /index.html. Its as simple as (resp/redirect target). No need to over complicate things.

    0 讨论(0)
  • 2020-12-28 14:49

    Just a consideration Binita, a gotcha I have been experiencing. Despite I couldn't find any documentation regarding importance of order defining Compojure routes I find out that this doesn't work

    (GET "/*" [] r/static) 
    (GET "/" [] (clojure.java.io/resource "public/index.html"))
    

    while this does work

    (GET "/" [] (clojure.java.io/resource "public/index.html"))
    (GET "/*" [] r/static) 
    

    Obviously the * matchs also the empty string but I thought that the order shouldn't matter at all.

    0 讨论(0)
  • 2020-12-28 14:52

    After viewing a lot of the answers here, I'm using the following code:

    (ns app.routes
      (:require [compojure.core :refer [defroutes GET]]
                [ring.util.response :as resp]))
    
    (defroutes appRoutes
      ;; ...
      ;; your routes
      ;; ...
      (GET "/" []
           (resp/content-type (resp/resource-response "index.html" {:root "public"}) "text/html"))))
    
    • No redirects;
    • No need of another middleware;
    • index.html stays in the right folder (resources/public/index.html);
    • It works with other middleware (a lot of the answers break when using with some ring default middleware);
    • It provides content-type so it works with wrap-content-type middleware.

    Check ring-defaults. It has best practices middleware you should use on your projects.

    0 讨论(0)
提交回复
热议问题