简体   繁体   English

从src目录提供图片文件-404错误

[英]Serving image files from src directory - 404 error

I created a site in Clojure , Pedestal and Boot Cljs . 我在ClojurePedestalBoot Cljs中创建了一个站点。 A tutorial I was using was this http://pedestal.io/guides/hello-world-content-types 我正在使用的教程是http://pedestal.io/guides/hello-world-content-types

Then I was trying to add an image to the site that was not shown in the tutorial. 然后,我试图将图像添加到本教程中未显示的站点。 I put the image a455.jpg to src folder. 我把图像a455.jpg放到src文件夹中。 I noticed that in a file build.boot a :resources-paths key is set to src folder. 我注意到在文件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$ 

So the src folder in this application is something like a public folder in common web applications. 因此, src在这个应用程序文件夹是像一个public共同的Web应用程序文件夹中。 Isn't it? 是不是

I tested it web browser, curl and Clojure response-for function. 我测试了它的Web浏览器,curl和Clojure response-for功能。 The site worked but without the image. 该网站正常运行,但没有图像。 Here you are curl results: 这是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)

And here you are the application 这是您的应用程序

(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))

How to make the image display on the site? 如何使图像显示在网站上?

Typically images and other items are put in the ./resources folder. 通常,图像和其他项目放在./resources文件夹中。 They may also be in subfolders, depending on the exact configuration in lein or boot . 它们也可能位于子文件夹中,具体取决于leinboot的确切配置。

Here is a sample lein project: http://git@gitlab.com:pedestal/hello.git 这是一个样例lein项目: http://git@gitlab.com:pedestal / hello.git

with a sample file ./resources/music.txt file. 带有示例文件./resources/music.txt文件。 These are typically read with (:require [clojure.java.io :as io]) as follows: 这些通常使用(:require [clojure.java.io :as io])读取,如下所示:

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

as you can see from the echo-intc in src/hello/core.clj . src/hello/core.cljecho-intc 可以看到 You can see the file access in action by issuing a lein run and then navigating your browser to localhost:8890 您可以通过lein run然后将浏览器导航到localhost:8890来查看文件访问的lein run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM