0

I am setting up a web application building a route and handler with Ring and Compojure. Every time I try lein ring server I get a 404 Not Found. But I should see

enter image description here

Edit After starting the server I am asked by IE to open or save the file. But Windows is not able to read the JSON file.

My project.clj looks like

(defproject web-viz 
 :dependencies [[org.clojure/clojure "1.4.0"]
                [ring/ring-core "1.1.7"]
                [ring/ring-jetty-adapter "1.1.7"]
                [compojure "1.1.3"]
                [hiccup "1.0.2"]]
:plugins [[lein-ring "0.8.3"]]
:ring {:handler web-viz.web/app})

and inside the src I have a file web.clj

(ns web-viz.web
(:require [compojure.route :as route]
      [compojure.handler :as handler]
      [clojure.string :as str])
(:use compojure.core
  ring.adapter.jetty
  [ring.middleware.content-type :only
  (wrap-content-type)]
  [ring.middleware.file :only (wrap-file)]
  [ring.middleware.file-info :only
  (wrap-file-info)]
  [ring.middleware.stacktrace :only
  (wrap-stacktrace)]
  [ring.util.response :only (redirect)]))

(defroutes site-routes
(GET "/" [] (redirect "/data/census-race.json"))
(route/resources "/")
(route/not-found "Page not found"))

(def app (-> (handler/site site-routes)
         (wrap-file "resources")
         (wrap-file-info)
         (wrap-content-type)))
John Wiseman
  • 3,081
  • 1
  • 22
  • 31
sunspots
  • 1,047
  • 13
  • 29
  • Look at the accepted answer here http://stackoverflow.com/questions/2483771/how-can-i-convince-ie-to-simply-display-application-json-rather-than-offer-to-do – KobbyPemson Apr 02 '14 at 22:17

3 Answers3

2

There should be a file with the content above located at

web-viz/resources/public/data/census-race.json
albusshin
  • 3,930
  • 3
  • 29
  • 57
  • I added the file public in between resources and data and this worked. But I get a page that says Windows can't open this file `census-race.json @albusshin – sunspots Apr 02 '14 at 21:55
2

You project is working for me. Here is how I structured the project

.
├── project.clj
├── resources
│   └── data
│       └── census-race.json
└── src
    └── web_viz
        └── web.clj
Scott
  • 1,648
  • 13
  • 21
1

I don't see anything obviously wrong but the following looks unusual:

(def app (-> (handler/site site-routes)
         (wrap-file "resources")
         (wrap-file-info)
         (wrap-content-type)))

From https://stackoverflow.com/a/22788463/894091:

You don't need any of the extra middleware like wrap-file, wrap-file-info, or wrap-content-type, since compojure.route/resources already does everything you need.

See if the following does the trick:

(def app
  (handler/site app-routes))
Community
  • 1
  • 1
deadghost
  • 5,017
  • 3
  • 34
  • 46
  • It seems to have worked. Except I get a page that says Windows can' open this file `census-race.json` @deadghost – sunspots Apr 02 '14 at 21:53
  • @sunspots what do you mean "can't open"? It might be your OS or browser. – deadghost Apr 02 '14 at 21:59
  • It's trying to open in Internet Explorer by first asking me to download the file @deadghost – sunspots Apr 02 '14 at 22:04
  • @sunspots if you can download the file, it's working. You might need to set your ring handler to send the data as text if you want it to look like the above in IE. Alternatively, try it in firefox. – deadghost Apr 02 '14 at 22:11
  • How would I set the handler to send the data as text? @deadghost – sunspots Apr 02 '14 at 22:12
  • If it is asking you to download the file then that means it worked. When IE doesn't know how to handle a mime-type `application/json` it downloads the file. – Scott Apr 02 '14 at 22:12
  • @sunspots https://github.com/weavejester/compojure/wiki/Routes-In-Detail See the very bottom. You'll probably want to set `Content-Type` to `text/plain`, but really it's already working as it should. – deadghost Apr 02 '14 at 22:18
  • If you want to test this quickly you can just change the file extension to *.txt* ring chooses based on the file extensions - https://github.com/mmcgrana/ring/wiki/Content-Types – Scott Apr 02 '14 at 22:27