Clojure Pedestal root用作应用程序八位字节流

Clojure Pedestal root用作应用程序八位字节流,第1张

概述我试图在 Pedestal 0.5.1中托管静态资产和服务.我使用:: file-path指向托管文件的目录.如果我直接导​​航到文件 http://localhost:8888/index.html,这可以正常工作,但如果我转到站点 http://localhost:8888的根目录,它将文件作为application / octet-stream而不是text / html提供.我改编了 He 我试图在 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:8888http/1.1 200 OKDate: Fri,18 Nov 2016 16:02:56 GMTLast-ModifIEd: Fri,18 Nov 2016 15:10:22 GMTContent-Type: application/octet-streamContent-Length: 12Server: Jetty(9.3.8.v20160314)hello world$curl -i localhost:8888/index.HTMLhttp/1.1 200 OKDate: Fri,18 Nov 2016 16:03:02 GMTLast-ModifIEd: Fri,18 Nov 2016 15:10:22 GMTContent-Type: text/HTMLContent-Length: 12Server: 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)]})

有关您可能想要包含的其他默认拦截器的示例,
见default-interceptors.

说明

这可能在实践中并不经常出现,因为许多网络
应用程序使用处理函数来生成主页
返回一个静态文件.

对于备用解决方案,您可以为/编写路由处理程序
使用适当的方法返回index.HTML的内容的路由
内容类型.

Pedestal的默认拦截器堆栈包括
io.pedestal.http.ring-middlewares/file
和io.pedestal.http.ring-middlewares/content-type.

这些拦截器只包含Ring中间件函数
file-request
和content-type-response,分别.

file-request返回一个java.io.file对象作为http响应.

content-type-response检查请求URI以确定
Content-Type标头的值.由于URI只是/它
默认为application / octet-stream.

相比之下,ring.mIDdleware.file-info(不推荐使用)进行了检查
响应中实际file对象的路径.
见file-info-response.

io.pedestal.http.ring-mIDdlewares / file-info是拦截器ring.mIDdleware.file-info / file-info-response周围的包装器.

总结

以上是内存溢出为你收集整理的Clojure Pedestal root用作应用程序/八位字节流全部内容,希望文章能够帮你解决Clojure Pedestal root用作应用程序/八位字节流所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1023690.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存