0

我正在尝试将环境变量设置为POWERED_BY变量message。然后我想测试message是空还是NULL。然后打印 "Powered by" message

目前,下面的代码不起作用。

(ns helloworld.web 
    (:use compojure.core [ring.adapter.jetty :only [run-jetty]] )
    (:require [compojure.route :as route]
              [compojure.handler :as handler]))


(defroutes main-routes
  ; what's going on
    (def message (System/getenv "POWERED_BY"))
    (GET "/" [] (apply str "Powered by " message))
    (route/resources "/")
    (route/not-found "Page not found")   )


(def app
    (handler/api main-routes))

(defn -main [port]
    (run-jetty app {:port (Integer. port)}))
4

1 回答 1

1

定义message外部路由定义:

 (def message (System/getenv "POWERED_BY"))

 (defroutes main-routes
  ; what's going on
 (GET "/" [] (str "Powered by " message)
 (route/resources "/")
 (route/not-found "Page not found"))

如果您想在每次收到请求时检索系统环境变量值,您可以使用以下let形式:

 (defroutes main-routes
  ; what's going on
 (GET "/" [] (let [message (System/getenv "POWERED_BY")]
                (str "Powered by " message))
 (route/resources "/")
 (route/not-found "Page not found"))

对于concat ,只需使用(str arg1 arg2 ...),apply适用于列表,所以如果你想使用它,你应该做类似的事情(apply str ["Powered by" message])

于 2013-10-28T20:01:20.933 回答