Clojure Pedestal root用作应用程序/八位字节流
我试图在
Pedestal 0.5.1中托管静态资产和服务.我使用:: file-path指向托管文件的目录.如果我直接导??航到文件
http://localhost:8888/index.html,这可以正常工作,但如果我转到站点
http://localhost:8888的根目录,它将文件作为application / octet-stream而不是text / html提供.我改编了
Hello World Sample,它有相同的行为.
SRC /程序hello_world / server.clj (ns hello-world.server (:require [io.pedestal.http :as http] [io.pedestal.http.route :as route]) (:gen-class)) (def routes (route/expand-routes [[]])) (def service {:env :prod ::http/join? false ::http/routes routes ::http/file-path "/tmp/www" ::http/type :jetty ::http/allowed-origins {:creds true :allowed-origins (constantly true)} ::http/port 8888}) (defonce runnable-service (http/create-server service)) (defn -main "The entry-point for 'lein run'" [& args] (println "nCreating your server...") (http/start runnable-service)) 开始莱恩跑 $curl -i localhost:8888 HTTP/1.1 200 OK Date: Fri,18 Nov 2016 16:02:56 GMT Last-Modified: Fri,18 Nov 2016 15:10:22 GMT Content-Type: application/octet-stream Content-Length: 12 Server: Jetty(9.3.8.v20160314) hello world $curl -i localhost:8888/index.html HTTP/1.1 200 OK Date: Fri,18 Nov 2016 16:03:02 GMT Last-Modified: Fri,18 Nov 2016 15:10:22 GMT Content-Type: text/html Content-Length: 12 Server: Jetty(9.3.8.v20160314) hello world 有没有办法修复“/”路由以提供正确的内容类型? 解决方法
要获取用作目录索引的文件的正确内容类型,
将拦截器io.pedestal.http.ring-middlewares / file-info添加到 您的基座配置. 这要求您使用覆盖默认拦截器链 例如,您的服务可能如下所示: (ns hello-world.service (:require [io.pedestal.http :as http] [io.pedestal.http.ring-middlewares :as middlewares] [io.pedestal.http.route :as route] [io.pedestal.http.route.definition :refer [defroutes]])) (defroutes routes [[]]) (def service {::http/type :jetty ::http/port 8080 ::http/interceptors [http/log-request http/not-found middlewares/session route/query-params (middlewares/file-info) ; HERE (middlewares/file "/tmp/www") ;; ... insert other interceptors ... (route/router #(deref #'routes) :map-tree)]}) 有关您可能想要包含的其他默认拦截器的示例, 说明 这可能在实践中并不经常出现,因为许多网络 对于备用解决方案,您可以为/编写路由处理程序 Pedestal的默认拦截器堆栈包括 这些拦截器只包含Ring中间件函数 file-request返回一个java.io.File对象作为HTTP响应. content-type-response检查请求URI以确定 相比之下,ring.middleware.file-info(不推荐使用)进行了检查 io.pedestal.http.ring-middlewares / file-info是拦截器ring.middleware.file-info / file-info-response周围的包装器. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |