Chris Edwards
Chris Edwards

Reputation: 1558

ring response downloads index.html instead of rendering it

I have an index.html located in resources/public/index.html and have defined the following routes (application is split up more than this, just making the code concise):

(ns example.example
  (:require [compojure.route :as route]))

(defroutes routes
           (GET "/" [] (resource-response "index.html" {:root "public"} "text/html")))

(defroutes application-routes
           routes
           (route/resources "/")
           (route/not-found (resource-response "index.html" {:root "public"} "text/html")))


(def application
  (wrap-defaults application-routes site-defaults))

However, when I go to localhost:8090/ it downloads the html file instead of rendering it.

If I go to localhost:8090/index.html it renders the file properly so I assumed my routing is incorrect somehow but after looking at examples I am not too sure why.

Upvotes: 3

Views: 956

Answers (2)

Ertuğrul Çetin
Ertuğrul Çetin

Reputation: 5231

Use this:

(:require [clojure.java.io :as io]
          [ring.middleware.resource :as resource])

(defroutes routes

     (GET "/" []
             (io/resource "index.html")))

Also use middleware for resource wrapping

(resource/wrap-resource "/public") 

Upvotes: 1

kongeor
kongeor

Reputation: 732

This is exactly the same issue with this question.

You need to create a middleware to update your request:

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

And then wrap your routes:

(def app
  (wrap-dir-index (wrap-defaults app-routes site-defaults)))

Complete handler.clj.

Upvotes: 2

Related Questions