Last active
August 16, 2022 12:08
-
-
Save bhauman/c63123a5c655d77c3e7f to your computer and use it in GitHub Desktop.
Helpful patterns when developing with ClojureScript Figwheel and Express js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 { }) |
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
@JimTheMan I've used figwheel-node, and added them to
package.json
, but you can also add them toproject.clj
in the compiler options (though that's been more experimental so far [experimental for me, that is; also, I'm a Leiningen user...):