Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve index.html at / by default in Compojure

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't. How can I make Compojure serve index.html when someone requests /?

Here's the code I'm using for the static directory:

; match anything in the static dir at resources/public (route/resources "/") 
like image 294
Kevin Burke Avatar asked Oct 11 '11 16:10

Kevin Burke


Video Answer


1 Answers

An alternative could be to create either a redirect or a direct response in an additional route. Like so:

(ns compj-test.core   (:use [compojure.core])   (:require [compojure.route :as route]             [ring.util.response :as resp]))  (defroutes main-routes   (GET "/" [] (resp/file-response "index.html" {:root "public"}))   (GET "/a" [] (resp/resource-response "index.html" {:root "public"}))   (route/resources "/")   (route/not-found "Page not found")) 

The "/" route returns a file response of "index.html" which is present in the public folder. The "/a" route responds directly by 'inlineing' the file index.html.

More on ring responses: https://github.com/mmcgrana/ring/wiki/Creating-responses

EDIT: removed unnecessary [ring.adapter.jetty] import.

like image 185
Paul Avatar answered Sep 25 '22 06:09

Paul