如何在Yada提供静态资源

时间:2019-04-19 04:16:50

标签: clojure yada

借助Compojure,我可以像这样提供静态资源:

(defroutes routes
  (route/resources "/"))

在Yada文档之后,我有此工作:

(def server
  (listener
    ["/"
     [["hello" (as-resource "Hello World!")]
      ["test" (resource {:produces "text/plain"
                         :response "This is a test!"})]
      [true (as-resource nil)]]]
    {:port 3000}))

但是如何让Yada从文件系统提供资源?

2 个答案:

答案 0 :(得分:3)

我最终在这里找到答案:Wrapping resource handlers with bidi

(ns yada-test
  (:require [yada.yada :refer [listener as-resource]]
            [bidi.ring :refer [resources]]))

(def server
  (listener
    ["/"
     [["" (resources {:prefix "public/"})]]]
    {:port 3001}))

答案 1 :(得分:0)

尝试以下操作:

(ns yada-test
  (:require [clojure.java.io :as io]
            [yada.yada :refer [listener as-resource]]]))

(def server
  (listener
    ["/"
     [["talks" (as-resource (io/file (io/resource "docs")))]
      [true (as-resource nil)]]]
    {:port 3001}))

您需要将资源目录添加到类路径并在其中创建docs文件夹。