0

我在ClojurePedestalBoot Cljs中创建了一个站点。我正在使用的教程是这个http://pedestal.io/guides/hello-world-content-types

然后我试图向网站添加一个教程中没有显示的图像。我把图像a455.jpg放到src文件夹中。我注意到在一个文件中,build.boot一个:resources-paths键被设置为src文件夹。

s@lokal:~/Dropbox$ tree ~/Dropbox/clojure-boot-heists//home/s/Dropbox/clojure-boot-heists/
├── build.boot
├── favicon.ico
├── profile.boot
├── project.clj
├── src
│   ├── a455.jpg
│   ├── favicon.ico
│   └── main.clj
└── target
    └── classes

--

s@lokal:~/Dropbox$ cat ~/Dropbox/clojure-boot-heists/build.boot 
(set-env!
 :resource-paths #{"src"}
 :dependencies   '[[onetom/boot-lein-generate "0.1.3" :scope "test"]
                   [io.pedestal/pedestal.service "0.5.1"]
                   [io.pedestal/pedestal.route   "0.5.1"]
                   [io.pedestal/pedestal.jetty   "0.5.1"]
                   [org.clojure/data.json        "0.2.6"]
                   [org.slf4j/slf4j-simple       "1.7.21"]])
(require 'boot.lein)
(boot.lein/generate)
s@lokal:~/Dropbox$ 

因此src,此应用程序中的文件夹类似于public普通 Web 应用程序中的文件夹。不是吗?

我测试了它的 web 浏览器、curl 和 Clojureresponse-for功能。该网站工作,但没有图像。这是curl结果:

s@lokal:~/Dropbox$ curl -si -H "Accept: text/html" http://localhost:8890
HTTP/1.1 200 OK
Date: Mon, 14 Jan 2019 23:22:09 GMT
Strict-Transport-Security: max-age=31536000; includeSubdomains
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Type: text/html
Transfer-Encoding: chunked
Server: Jetty(9.3.8.v20160314)

<!doctype html>
<html lang='pl'>
<head>
<meta charset='utf-8'>
<title>Hi world!</title>
</head>
<body>
<img src='a455.jpg'>
</body>
</html>

--

s@lokal:~/Dropbox$ curl -si -H "Accept: text/html" http://localhost:8890/a455.jpg
HTTP/1.1 404 Not Found
Date: Mon, 14 Jan 2019 23:22:27 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Server: Jetty(9.3.8.v20160314)

在这里你是应用程序

(ns main
  (:require [clojure.data.json :as json]
            [io.pedestal.http :as http]
            [io.pedestal.http.route :as route]
            [io.pedestal.http.content-negotiation :as conneg]))

(defn ok [body]
  {:status 200 :body body})

(defn not-found []
  {:status 404 :body "Not found\n"})

(defn main-for [nm]
  (cond
    (unmentionables nm) nil
    (empty? nm) "<!doctype html>
<html lang='pl'>
<head>
<meta charset='utf-8'>
<title>Hi world!</title>
</head>
<body>
<img src='a455.jpg'>
</body>
</html>
"
    :else (str "Hello, " nm "\n")))


(defn respond-main [request]
  (let [nm (get-in request [:query-params :name])
        resp (main-for nm)]
    (if resp
      (ok resp)
      (not-found))))

(def supported-types ["text/html" "application/edn" "application/json" "text/plain"])
(def content-neg-intc (conneg/negotiate-content supported-types))
(defn accepted-type
  [context]
  (get-in context [:request :accept :field] "text/plain"))

(defn transform-content
  [body content-type]
  (case content-type
    "text/html" body
    "text/plain" body
    "application/edn" (pr-str body)
    "application/json" (json/write-str body)))

(defn coerce-to
  [response content-type]
  (-> response
      (update :body transform-content content-type)
      (assoc-in [:headers "Content-Type"] content-type)))

(def coerce-body
  {:name ::coerce-body
   :leave
         (fn [context]
           (cond-> context
                   (nil? (get-in context [:response :headers "Content-Type"]))
                   (update-in [:response] coerce-to (accepted-type context))))})

(def routes
  (route/expand-routes
    #{["/" :get [coerce-body content-neg-intc respond-main] :route-name :main]}))

(def service-map
  {::http/routes routes
   ::http/type   :jetty
   ::http/port   8890})

(defn start []
  (http/start (http/create-server service-map)))

(defonce server (atom nil))

(defn start-dev []
  (reset! server
          (http/start (http/create-server
                        (assoc service-map
                          ::http/join? false)))))

(defn stop-dev []
  (http/stop @server))

(defn restart []
  (stop-dev)
  (start-dev))

如何使图像在网站上显示?

4

1 回答 1

0

通常图像和其他项目都放在./resources文件夹中。它们也可能位于子文件夹中,具体取决于lein或中的确切配置boot

这是一个示例 lein 项目: http://git@gitlab.com:pedestal/hello.git

带有示例文件./resources/music.txt文件。这些通常读(:require [clojure.java.io :as io])作如下:

(slurp (io/resource "music.txt"))

从里面可以看出echo-intcsrc/hello/core.clj您可以通过发出 alein run然后在浏览器中导航到localhost:8890

于 2019-01-15T03:18:01.103 回答