Skip to content

Instantly share code, notes, and snippets.

@bhauman
Last active August 16, 2022 12:08
Show Gist options
  • Save bhauman/c63123a5c655d77c3e7f to your computer and use it in GitHub Desktop.
Save bhauman/c63123a5c655d77c3e7f to your computer and use it in GitHub Desktop.
Helpful patterns when developing with ClojureScript Figwheel and Express js
(ns todo-server.core
(:require
[cljs.nodejs :as nodejs]
[figwheel.client :as fw]))
(nodejs/enable-util-print!)
(defonce express (nodejs/require "express"))
(defonce serve-static (nodejs/require "serve-static"))
(defonce http (nodejs/require "http"))
;; app gets redefined on reload
(def app (express))
;; routes get redefined on each reload
(. app (get "/hello"
(fn [req res] (. res (send "Hello world")))))
(. app (use (serve-static "resources/public" #js {:index "index.html"})))
(def -main
(fn []
;; This is the secret sauce. you want to capture a reference to
;; the app function (don't use it directly) this allows it to be redefined on each reload
;; this allows you to change routes and have them hot loaded as you
;; code.
(doto (.createServer http #(app %1 %2))
(.listen 3000))))
(set! *main-cli-fn* -main)
(fw/start { })
@JimLynchCodes
Copy link

Nice! this is probably a very n00b question, but how to you add javascript dependencies to a clojurescript project (with or without leiningen)?

@luskwater
Copy link

luskwater commented Oct 26, 2017

@JimTheMan I've used figwheel-node, and added them to package.json, but you can also add them to project.clj in the compiler options (though that's been more experimental so far [experimental for me, that is; also, I'm a Leiningen user...):

:compiler
 {:main explore-resolve.core
  :asset-path "target/js/compiled/dev"
  :output-to "target/js/compiled/explore_resolve.js"
  :output-dir "target/js/compiled/dev"
  :target :nodejs
  :npm-deps
  {:config "^1.27.0"
   :express "^4.16.2"
   :serve-static "^1.13.1"}
  :install-deps true
  :optimizations :none
  :source-map-timestamp true}

@theronic
Copy link

theronic commented Oct 4, 2019

Thanks for this. How would you unit test against a defined route without running a server?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment