sitefox.web

Functions to start the webserver and create routes.

add-default-middleware

(add-default-middleware app)

Set up default express middleware for:

  • Writing rotating logs to logs/access.log.
  • Setting up sessions in the configured database.
  • Parse cookies and body.

build-absolute-uri

(build-absolute-uri req path)

Creates an absolute URL including host and port. Use inside a route: (build-absolute-uri req "/somewhere")

create

(create)

Create a new express app and add the default middleware.

get-named-route

(get-named-route req route-name)

Retrieve a route that has previously been named.

is-post?

(is-post? req)

Check whether an express request uses the POST method.

name-route

(name-route app route route-name)

Attach a name to a route that can be recalled with get-named-route.

reset-routes

(reset-routes app)

Remove all routes in the current app and re-add the default middleware. Useful for hot-reloading code.

serve

(serve app)

Start serving an express app.

Configure BIND_ADDRESS and PORT with environment variables. They default to 127.0.0.1:8000.

setup-error-handler

(setup-error-handler app template selector view-component & [error-handler-fn])

Sets up an express route to handle 404 and 500 errors. This must be the last handler in your server:

(defn setup-routes [app]
  (web/reset-routes app)
  ; ... other routes
  ; 404 and 500 error handling
  (web/setup-error-handler app template "main" component-error-page email-error-callback))

Pass it the express app, an HTML string template, query selector and a Reagent view-component. Optionally pass error-handler-fn which will be called on every 500 error with args req, error. The error view-component will be rendered and inserted into the template at selector.

The error component will receive three arguments: * req - the express request object. * error-code - the exact error code that occurred (e.g. 404, 500 etc.). * error - the error object that was propagated (if any).

Example:

(make-error-handler app my-template "main" my-error-component)

To have all 500 errors emailed to you and logged use tracebacks/install-traceback-handler and pass it as error-handler-fn.

start

(start)

Create a new express app and start serving it. Runs (create) and then (serve) on the result. Returns a promise which resolves with app host port server once the server is running.

static-folder

(static-folder app route dir)

Express middleware to statically serve a dir on a route relative to working dir.

strip-slash-redirect

(strip-slash-redirect req res n)

Express middleware to strip slashes from the end of any URL by redirecting to the non-slash version. Use: `(.use app strip-slash-redirect)