Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should javascript be integrated into a Clojure/Ring web app?

How are Javascript resources best integrated into a ring application?

Specifically:

  • Where would we put our .js files?
  • How and where should unit tests be set up?
  • What is the best way to set up acceptance tests for functionality that cuts across the client and server sides?

Are there any best practices for javascript with ring applications? One possible answer would be to develop the client and server sides completely separately, to essentially separate everything into two separate projects, but I'm not completely happy with that idea.

(I'm also aware of clojurescript, though I'm thinking mainly of javscript code that's been written as javascript.)

like image 871
Rob Lachlan Avatar asked Mar 15 '11 19:03

Rob Lachlan


1 Answers

Ring has support to serve files directly from a folder (ring.middleware.file or ring.middleware.static which is what I would use) or from a resource in the jar/war. Your best bet is to use these mechanisms to serve your static (javascript/images) content. If you have the freedom to do this, I will put in a route similar to this one to serve all HTTP requests at /static/ from such a folder:

(def *route*
    (ring/wrap-static "c:/statics/" ["/static/"]))

Once you know how to handle a request for a static resource (like a javascript resource) it's the same as with anything else like PHP or ASP.

Another alternative is to define your resource routes as per normal, and then in the "catch-all" (normally something like this (ring/GET * request (handle-static-request request)) route, handle any remaining unserviced request with a static request.

like image 118
Pieter Breed Avatar answered Sep 17 '22 08:09

Pieter Breed