Mini tutorial to serve an image needed (Hello Pic)

143 views
Skip to first unread message

allegro....@gmail.com

unread,
Jan 15, 2019, 7:50:47 AM1/15/19
to pedestal-users
I am a newbe. I have practised tutorials on a site http://pedestal.io/guides/

Now I have started to add an image to a site I am building in Pedestal. I always get `Not Found` error for the pic.

I am looking for a small tutorial of add pics to the site. I mean static site. So anything to make `<img src="any_image.jpg">` html work.

Phill Wolf

unread,
Jan 15, 2019, 8:13:43 PM1/15/19
to pedestal-users
In Pedestal you do not need special incantations for images.  If your program can respond to any particular url, such as /hello.html, then you could use the same technique to respond to /any_image.jpg.

For general grounding, I recommend "Web Development with Clojure" by Dmitri Sotnikov.  I have the 1st edition.  Images are on page 40.  The book is not about Pedestal exactly, but if your sample app can serve /hello.html then you have the Pedestal aspects pretty well in hand.

Paul deGrandis

unread,
Jan 16, 2019, 6:15:15 AM1/16/19
to pedestal-users
Hi Janek,

Welcome to using Pedestal and congrats on making your way through the guides!

In your project, you most likely have a public resources directory for putting things like HTML files and images.  It's typically found at resources/public within your Pedestal project.
The resource interceptor will always check to see if there's a resource that matches the URL set by a service's resource-path setting.  This interceptor is always included in the default interceptors.

If you place an image at resources/public/some_img.jpg and you make a request to http://localhost:8080/public/some_img.jpg you should see your image.

I hope this helps!

Cheers,
Paul

janek mazurowski

unread,
Jan 16, 2019, 4:32:47 PM1/16/19
to pedestal-users
Hi Paul,
thanks for the answer. It helped! Now my code is this:

(ns main
 
(:require
    [io.pedestal.http.ring-middlewares]
   
[io.pedestal.http.route :as route]
   
[io.pedestal.http :as http]
   
))

(def routes
 
(route/expand-routes []))

(def service-map
 
{
   
::http/resource-path "/public"
   ::http/routes        routes
   
::http/type          :jetty
   ::http/port          8890
   })



(defn start []
 
(http/start (http/create-server service-map)))

(defonce server (atom nil))

(defn start-dev []
 
(reset! server
         
(http/start (http/create-server
                        (assoc service-map
                         
::http/join? false)))))

(defn stop-dev []
 
(http/stop @server))

(defn restart []
 
(stop-dev)
 
(start-dev))
I successfully tested it by curl:
janek@debian:~/proj/clojure-resources$ curl -sI -X GET http://localhost:8890/images/foo.jpg | grep 'HTTP/1.1'
HTTP/1.1 200 OK


janek mazurowski

unread,
Jan 16, 2019, 4:36:53 PM1/16/19
to pedestal-users
Thanks for the answer, Phill

ChillzPillz

unread,
Mar 20, 2021, 3:53:28 PM3/20/21
to pedestal-users
Hi Paul,

I am new too with both pedestal and clojure. I like pedestal a lot but this exact similar problem is making me reconsider. 


I am trying to host a javascript file. And this simply doesn't seem to be working.

Is this something you can help me with?

I am sorry to be bothering you. I have the resource-path settings already. It doesn't work. I am missing something obvious.

I'll greatly appreciate your help on this.

Thanks,
Chillz

周幕卫

unread,
Jul 15, 2022, 11:37:27 AM7/15/22
to pedestal-users
Can you post your code here?
Reply all
Reply to author
Forward
0 new messages