Created
December 26, 2014 16:16
-
-
Save niwinz/1adabcc759e125a587f7 to your computer and use it in GitHub Desktop.
Lightweight sugar syntax for expressjs with clojurescript.
This file contains 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 express | |
"Ligweight interface to requirejs." | |
(:refer-clojure :exclude [set get]) | |
(:require [cljs.nodejs :as node])) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Constants | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(def ^{:doc "Global express import." | |
:private true} | |
express (node/require "express")) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Constructors | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(defn app | |
"Create a express app instance." | |
[] | |
(express)) | |
(defn router | |
"Create a express router instance." | |
[& [opts]] | |
(.Router express (clj->js opts))) | |
(defn routes | |
"Create a router instance and attach | |
handlers to it." | |
[& args] | |
(let [app (router)] | |
(reduce (fn [app [verb path handler]] | |
(condp = verb | |
:all (.all app path handler) | |
:get (.get app path handler) | |
:post (.post app path handler) | |
:put (.put app path handler) | |
:delete (.delete app path handler) | |
app)) | |
app | |
args))) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Request & Response Helpers | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(defn send | |
[res data] | |
(.send res data)) | |
(defn status | |
[res code] | |
(.status res code)) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Helpers | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
(defn use | |
"Attach middlware to express app." | |
([app handler] | |
(.use app handler)) | |
([app path handler] | |
(.use app path handler))) | |
(defn listen | |
"Shortcut for start http server." | |
([app port] | |
(.listen app port)) | |
([app port callback] | |
(.listen app port callback))) | |
(defn set | |
[app key value] | |
(.set app key value) | |
app) | |
(defn get | |
[app key] | |
(.get app key)) | |
(defn enable | |
[app key] | |
(.enable app key) | |
app) | |
(defn disable | |
[app key] | |
(.disable app key) | |
app) | |
(defn enabled? | |
[app key] | |
(.enabled app key)) | |
(defn disabled? | |
[app key] | |
(.disabled app key)) |
This file contains 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 myns | |
(:require [cljs.nodejs :as node] | |
[express :as ex])) | |
(node/enable-util-print!) | |
(defn handler1 | |
[req res] | |
(-> res | |
(ex/status 200) | |
(ex/send "Hello world"))) | |
(def routes | |
(ex/routes | |
[:get "/" handler1] | |
[:all "/foo/" handler1])) | |
(defn -main | |
[] | |
(-> (ex/app) | |
(ex/use "/" routes) | |
(ex/listen 3000))) | |
(set! *main-cli-fn* -main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment