Skip to content

Instantly share code, notes, and snippets.



Last active Jan 28, 2021
What would you like to do?
Helpful patterns when developing with ClojureScript Figwheel and Express js
(ns todo-server.core
[cljs.nodejs :as nodejs]
[figwheel.client :as fw]))
(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 { })

This comment has been minimized.

Copy link

@JimLynchCodes JimLynchCodes commented Oct 16, 2017

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


This comment has been minimized.

Copy link

@luskwater 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...):

 {: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
  {:config "^1.27.0"
   :express "^4.16.2"
   :serve-static "^1.13.1"}
  :install-deps true
  :optimizations :none
  :source-map-timestamp true}

This comment has been minimized.

Copy link

@theronic 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