如何在雅达服务静态资源
How to serve static resources in 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 从文件系统提供资源?
尝试以下操作:
(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
文件夹。
我最终在这里找到了答案:
(ns yada-test
(:require [yada.yada :refer [listener as-resource]]
[bidi.ring :refer [resources]]))
(def server
(listener
["/"
[["" (resources {:prefix "public/"})]]]
{:port 3001}))
借助 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 从文件系统提供资源?
尝试以下操作:
(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
文件夹。
我最终在这里找到了答案:
(ns yada-test
(:require [yada.yada :refer [listener as-resource]]
[bidi.ring :refer [resources]]))
(def server
(listener
["/"
[["" (resources {:prefix "public/"})]]]
{:port 3001}))