-->
Develop web applications using Sitefox ClojureScript framework with Node.js for streamlined setups
Sitefox is a ClojureScript web framework for Node.js, designed for rapid development with batteries included. It uses Express.js under the hood with sensible defaults.
(ns webserver
(:require
[promesa.core :as p]
[sitefox.web :as web]
[sitefox.html :refer [render]]))
(defn root [_req res]
(->> (render [:h1 "Hello world!"])
(.send res)))
(p/let [[app host port] (web/start)]
(.get app "/" root)
(print "Serving on" (str "http://" host ":" port)))
(ns myapp.db
(:require [sitefox.db :as db]
[promesa.core :as p]))
;; Key-value store
(p/let [table (db/kv "sometable")
_ (.set table "key" "42")
val (.get table "key")]
(println "Value:" val))
;; Direct SQL
(p/let [client (db/client)
rows (.query client "SELECT * FROM sometable WHERE x = 1")]
(println rows))
(defn setup-routes [app]
(let [template (fs/readFileSync "index.html")]
(web/reset-routes app)
(auth/setup-auth app)
(auth/setup-email-based-auth app template "main")
(auth/setup-reset-password app template "main")))
(ns myapp.views
(:require [sitefox.html :refer [render-into]]))
(defn component-main []
[:div
[:h1 "Hello world!"]
[:p "Server-side rendered content"]])
(def html-string
(render-into (fs/readFileSync "index.html")
"main"
[component-main]))
;; Server-side form component
[:form {:method "POST" :action "/submit"}
[:input {:name "_csrf"
:type "hidden"
:default-value (.csrfToken req)}]
[:input {:name "email" :type "email"}]]
;; Client-side AJAX POST
(-> (fetch-csrf-token)
(.then (fn [token]
(js/fetch "/api/endpoint"
#js {:method "POST"
:headers #js {:Content-Type "application/json"
:X-XSRF-TOKEN token}
:body (js/JSON.stringify data)}))))
(mail/send-email
"to@example.com"
"from@example.com"
"Email Subject"
:text "Email body text")
PORT
- Web server portBIND_ADDRESS
- Web server bind address SMTP_SERVER
- SMTP server URLDATABASE_URL
- Database connection URL (default: SQLite)SEND_CSRF_TOKEN
- Enable cookie-based CSRF tokens
Add this context to your project via the
ctxs
command line integration: