Clojurescript-node.js mount module for a district server, that takes care of HTTP server and its endpoints. This module currently utilises expressjs as an underlying HTTP server.
Add [district0x/district-server-endpoints "1.0.3"]
into your project.clj
Include [district.server.endpoints]
in your CLJS file, where you use mount/start
Warning: district0x modules are still in early stages, therefore API can change in a future.
To see how district server modules play together in real-world app, you can take a look at NameBazaar server folder, where this is deployed in production.
You can pass following args to endpoints module:
:port
Port at which HTTP server will start:middlewares
Collection of expressjs middlewares you want to install. See list of district-server-middlewares.:default-middlewares
This module comes with few default middlewares. Each has an unique keyword key. If you want to use only some of default ones, you can pass collection of their keys here.
(ns my-district
(:require [mount.core :as mount]
[district.server.endpoints :refer [reg-get! send query-params]]))
(reg-get! "/my-endpoint"
(fn [req res]
(if (:hi? (query-params req))
(send res {:greeting "Hello"})
(send res 400 "Bad Request"))))
(-> (mount/with-args
{:endpoints {:port 6200}})
(mount/start))
You need to define your endpoints with reg-get!
, reg-post!
, etc. before you start mount. Endpoint definitions can be of course in different file and you just require it in main file.
For convenient parsing of query params values, we recommend using district-parsers, because Clojure types get lost along the way if you use standard format of query params.
district-server-endpoints
gets initial args from config provided by district-server-config/config
under the key :endpoints
. These args are then merged together with ones passed to mount/with-args
.
If you wish to use custom modules instead of dependencies above while still using district-server-endpoints
, you can easily do so by mount's states swapping.
This namespace contains following functions for working with expressjs HTTP server:
Sends server response. Function chooses format either transit or json automatically, based on request's "Accept" header.
Sets status of response.
Gets query params of request.
Gets route params of request.
Gets body of request.
Registers GET endpoint.
Registers POST endpoint.
Registers PUT endpoint.
Registers DELETE endpoint.
Registers endpoint of any method that's supported by expressjs.
Default middlewares that comes with endpoints module are defined here. If you want to use only subset of them, choose from the following and pass it to :default-middlewares
at mount start.
:middleware/cors
cors middleware:middleware/urlencoded
urlencoded body parser:middleware/text-body-parser-for-transit
passes body for "application/transit+json" as text:middleware/transit-body-parser
parses transit body:middleware/query-params-parser
parses query params
# To start REPL and run tests
lein deps
lein repl
(start-tests!)
# In other terminal
node tests-compiled/run-tests.js
# To run tests without REPL
lein doo node "tests" once