routes not working for me

17 views
Skip to first unread message

michaelr524

unread,
Jun 23, 2010, 8:34:19 AM6/23/10
to Compojure
Hi

Please tell me what I'm doing wrong.
Here is my project file:
:dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]
[compojure "0.4.0-RC3"]
[ring/ring-jetty-adapter "0.2.3"]
[clojure-http-client "1.1.0-SNAPSHOT"]
]

Here is my code:

(ns dataentry.formeditor
(:use compojure.core, ring.adapter.jetty)
(:require [compojure.route :as route])
(:require [clojure.contrib.duck-streams :as streams]))

(defroutes main-routes
(GET "/" [] (streams/slurp* "resources/html/form_editor.html"))
(GET "/resources/html/*" [params] (or (println params) :next))
(route/not-found "<h1>Page not found</h1>"))

(run-jetty main-routes {:port 8080})


What is happening is the html page I'm loading contains links to CSS
and JS files which get requested next by the browser.
My problem is that the params are always nil if I use *.
And if I do something like:
(GET "/resources/html/:filename" [filename] (or (println
filename) :next))

This route is never being recognized.

Please help, I'm struggling with this for a few hours already :)

Thanks
Michael

James Reeves

unread,
Jun 23, 2010, 9:39:38 AM6/23/10
to comp...@googlegroups.com
On 23 June 2010 13:34, michaelr524 <micha...@gmail.com> wrote:
> Please tell me what I'm doing wrong.
...

>
> (defroutes main-routes
>  (GET "/" [] (streams/slurp* "resources/html/form_editor.html"))
>  (GET "/resources/html/*" [params] (or (println params) :next))
>  (route/not-found "<h1>Page not found</h1>"))

The second argument of a route defines the bindings. If you use a
vector, it binds directly against the parameters. If you use anything
else, it binds against the request map. So you could write:

(GET "/resources/html/*" {params :params}
(do-something-to (params "*")))

However, perhaps a better solution is to use a keyword with a custom
regular expression:

(GET ["/resources/html/:filename" :filename #".*"] [filename]
(do-something-to filename))

Note that you no longer need :next to go to the next route. Instead,
just return nil to indicate the route doesn't match.

Another thing you might want to do is instead of using slurp, just
return a File object:

(GET "/" [] (streams/file "resources/html/form_editor.html"))

This should result in the file being streamed to the response, without
the need to put it into a string first of all.

Finally, if you're just sending files, you might want to look at the
static-files route:

(route/static-files "/resources/html" {:root "resources/html"})

- James

Nebojsa Stricevic

unread,
Jun 23, 2010, 9:41:58 AM6/23/10
to comp...@googlegroups.com
Hello,

This code works for me:

(ns hello_www

 (:use compojure.core, ring.adapter.jetty)
 (:require [compojure.route :as route])
 (:require [clojure.contrib.duck-streams :as streams]))

(defroutes main-routes
 (GET "/resources/html/:filename" [filename]
      (or (println filename) :next))
 (route/not-found "<h1>Page not found</h1>"))

(run-jetty main-routes {:port 8080})

When i put "http://localhost:8080/resources/html/test" in browser address bar i get "test" on repl and "Page not found" in browser.


--
You received this message because you are subscribed to the Google Groups "Compojure" group.
To post to this group, send email to comp...@googlegroups.com.
To unsubscribe from this group, send email to compojure+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/compojure?hl=en.




--
Nebojša Stričević

michaelr524

unread,
Jun 23, 2010, 11:36:41 AM6/23/10
to Compojure
Hi James,

route/files worked perfectly for me. Thanks!

Nebojša, I checked and it works for me too. The problem is that my
files have .css and .js suffixes and it didn't work. I guess the right
usage would be as James suggested to use a keyword and a regex.

Btw, another question: Is this the right solution for what it tries to
achieve?

(GET "/" [] (streams/file "resources/html/form_editor.html"))

It's supposed to say: If someone accesses the website root then
display this page without redirecting with http.

Thanks

On Jun 23, 4:39 pm, James Reeves <jree...@weavejester.com> wrote:

James Reeves

unread,
Jun 23, 2010, 11:40:16 AM6/23/10
to comp...@googlegroups.com
On 23 June 2010 16:36, michaelr524 <micha...@gmail.com> wrote:
> (GET "/" [] (streams/file "resources/html/form_editor.html"))
>
> It's supposed to say: If someone accesses the website root then
> display this page without redirecting with http.

Yes. Ring, and by extension Compojure, allows the body of a request to
be a file or stream. If it is, it pipes the output directly to the
response. This is more efficient than slurping the file into a string.

- James

Reply all
Reply to author
Forward
0 new messages