Skip to content

Middlewares

LiuTao edited this page Nov 10, 2016 · 4 revisions

Order of calling middlewares

In the configuration file named middlewares under the config/ directory, each line contains a full qualified name of function will be called when a request come in. The calling order is from top to down, each middlewares, except the first one, will be invoked depends on its previous one.

Calling convention

Every function as middlewares must be the following signature

((req eloquent.mvc.request:<request>) (next function) &key config)

When a middleware function is called, the parameter next will be filled a function which when is called also, will call the next function under the current one in the configuration file.

Built-in middlewares

access-log

For every request, collects information from both request and response, and write to log files under directory /log/. The structure of one line log message is similar the the access log of NGINX

compress

For every request, if the value of the header "Accept-Encoding" includes sub-string "gzip", this function will encode the original response body into bytes in GZIP format, and return to client.

handle-error

For every request, if there is a condition(exception in other languages) signaled by the NEXT procedures, it will be catched by this middleware and converted to a response of HTTP code 500, and the backtrace of this condition as its HTTP body, send back to client.

parse-body

For every request, if the value of header "Content-Type" is recognized, then the body of incomming HTTP body will be parsed by a suitable parser. Now this middleware only support the following Content-Type:

  • application/json
  • application/x-www-form-urlencoded

static-file

For every request, if the path of it starts with a prefix specified in the configuration file's static-file section, this middleware will construct a path for file under directory /static/, return this pathname if exists, or respond 404 if file doesn't exist

Clone this wiki locally