使用Ring和Compojure提供数据 [英] Serving data with Ring and Compojure

查看:196
本文介绍了使用Ring和Compojure提供数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在配置和设置一个网络应用程序来提供JSON数据 http://www.ericrochester.com/clj-data-analysis/data/census-race.json 文件。

I am configuring and setting up a web application to serve the JSON data http://www.ericrochester.com/clj-data-analysis/data/census-race.json file statically.

我的依赖项:

  :dependencies [[org.clojure/clojure "1.5.1"]
                 [org.clojure/clojurescript "0.0-2197"]
                 [ring/ring-core "1.1.7"]
                 [ring/ring-jetty-adapter "1.1.7"]
                 [compojure "1.1.3"]
                 [hiccup "1.0.2"]
                 [lein-cljsbuild "0.2.10"]]

由于标题说我使用Ring作为开发插件,即

As the title says I'm using Ring as a development plugin, i.e.

  :plugins [[lein-ring "0.8.3"]]

Leiningen项目是

The Leiningen project is

   (ns test-app.core
   (:require [compojure.route :as route]
             [compojure.handler :as handler]
             [clojure.string :as str])
   (:use compojure.core
             ring.adapter.jetty
             [ring.middleware.content-type :only
             (wrap-content-type)]
             [ring.middleware.file :only (wrap-file)]
             [ring.middleware.file-info :only
             (wrap-file-info)]
             [ring.middleware.stacktrace :only
             (wrap-stacktrace)]
             [ring.util.response :only (redirect)]))



and in the project.clj

  :ring {:handler test-app.core/app}

我不知道这是否会告诉Ring web应用程序
是。

which I am not sure if this will tell Ring where the web application is.

我不知道如何静态地提供JSON数据文件。我已经读过,Ring在你的项目的/ resources目录中提供静态文件,在这种情况下,创建目录/ resources / data,并把你下载的数据文件放在 http://www.ericrochester.com/clj-data-analysis/data/census-race.json into into。

Moreover, I am not sure how to serve the JSON data file statically. I have read that "Ring serves static files out of the /resources directory of your project. In this case, create the directory /resources/data and put the data file that you downloaded from http://www.ericrochester.com/clj-data-analysis/data/census-race.json into it."

这主要是关于创建 / resources / data 目录的最后一部分,我在实现中丢失了。

It is mostly this last part about creating the /resources/data directory where I am lost in the implementation. Could someone show how this part is done?

如果我可以学会越过这个我正在寻找构建路线和处理程序,即

If I can learn to get past this I am looking to build routes and handlers, i.e.

  (defroutes site-routes
     (GET "/" [] (redirect "/data/census-race.json"))
     (route/resources "/")
     (route/not-found "Page not found"))


$ b b

and

 (def app
    (-> (handler/site site-routes)
    (wrap-file "resources")
    (wrap-file-info)
    (wrap-content-type)))


推荐答案


和project.clj

and in the project.clj

:ring {:handler test-app.core / app}

:ring {:handler test-app.core/app}

我不知道这会告诉Ring web应用程序
是。 / p>

which I am not sure if this will tell Ring where the web application is.

当您运行 $ lein ring server-headless 时,它将查找 app test-app.core 命名空间中。您应该在 core.clj 文件中有类似以下内容:

When you run $ lein ring server-headless it will look for app in the test-app.core namespace. You should have something like the following in your core.clj file:

(def app
  (handler/site app-routes))




这主要是关于创建/ resources / data
目录的最后一部分,我在实现中丢失了。

It is mostly this last part about creating the /resources/data directory where I am lost in the implementation. Could someone show how this part is done?

在项目目录的根目录下应该有一个 resources 文件夹。在 resources 文件夹中创建数据文件夹。

In the root of your project directory you should have a resources folder. Create a data folder inside the resources folder.

我不知道你有什么问题?

I'm not sure what problem you're having?

这篇关于使用Ring和Compojure提供数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆