webserverclojureringbidi

Simple web app of Bidi, Ring and Lein gives a 500 error


I am learning Clojure for the web. I am trying to implement Bidi and Ring in Lein app. When I open the site I get a 500 error:

HTTP ERROR: 500

Problem accessing /. Reason:

Response map is nil

Powered by Jetty://

My files are the following:

./project.clj

(defproject bidi-ring "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [bidi "2.1.6"]
                 [ring/ring-core "1.6.3"]
                 [ring/ring-jetty-adapter "1.6.3"]]
  :repl-options {:init-ns my.app}
  :main my.app)

./src/my/handler.clj

(ns my.handler (:gen-class)
    (:require [bidi.ring :refer (make-handler)]
              [ring.util.response :as res]))
(defn index-handler
  [request]
  (res/response "Homepage"))
(defn article-handler
  [{:keys [route-params]}]
  (res/response (str "You are viewing article: " (:id route-params))))
(def handler
  (make-handler ["/" {"index.html" index-handler
                      ["articles/" :id "/article.html"] article-handler}]))

./src/my/app.clj

(ns my.app (:gen-class)
    (:require [my.handler :refer [handler]]
              [ring.middleware.session :refer [wrap-session]]
              [ring.middleware.flash :refer [wrap-flash]]))
(use 'ring.adapter.jetty)

(defn -main []

  (def app
    (-> handler
        wrap-session
        wrap-flash))

  
  (run-jetty app {:port 3000}))

How to fix it?


Solution

  • You have made a mistake in your routes. Look at this Bidi demo repo:

    https://github.com/io-tupelo-demo/bidi

    The code on line 16 of tst.demo.core shows what is happening. In bidi, a route "/index.html" does not include the degenerate route "/":

      (let [routes   ["/" ; common prefix
                      {"index.html"   :index
                       "article.html" :article}]
    
            routes-2 ["/" ; common prefix
                      {""             :index ; add a default route so "/" and "/index.html" both => `:index` handler
                       "index.html"   :index
                       "article.html" :article}]] 
        (is= (bidi/match-route routes "/index.html") {:handler :index}) ; normal case
    
        (is= (bidi/match-route routes "/") nil) ; plain "/" route is not available, has no default
    
        (is= (bidi/match-route routes-2 "/") {:handler :index})) ; Now we get the :index route
    

    When no route matches, match-route returns a nil, which the server then translates into a 500 not-found that is sent to the browser.