-
Notifications
You must be signed in to change notification settings - Fork 91
Using middleware
Each incoming request passes through your server's middleware stack. You may specify your own personal stack (via --stack
) or work with the built-in default.
Name | Description |
---|---|
↓ Body Parser | Parses the request body, making ctx.request.body available to downstream middleware. |
↓ Request Monitor | Feeds traffic information to the --verbose output. |
↓ Log | Outputs an access log or stats view to the console. |
↓ Cors | Support for adding Cross-Origin Resource Sharing (CORS) headers |
↓ Json | Pretty-prints JSON responses. |
↓ Rewrite | URL Rewriting. Use to re-route requests to local or remote destinations. |
↓ Blacklist | Forbid access to sensitive or private resources |
↓ Conditional Get | Support for HTTP Conditional requests. |
↓ Mime | Customise the mime-type returned with any static resource. |
↓ Compress | Compress responses using gzip. |
↓ Mock Response | Mock a response for any given request. |
↓ SPA | Support for Single Page Applications. |
↓ Static | Serves static files. |
↓ Index | Serves directory listings. |
Local-web-server uses Koa for its middleware stack. See here for a guide explaining how Koa middleware works and why stack order is significant. See here for the ctx
object documentation.
A personalised stack can be created by passing middleware module names to --stack
. If all you need is to serve static files, the following command is all you need.
$ ws --stack lws-static
Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
The lws-
prefix in the module name is optional, the following command works the same.
$ ws --stack static
Serving at http://mbp.local:8000, http://127.0.0.1:8000, http://192.168.0.100:8000
You may browse any file in the current folder, e.g. http://127.0.0.1:8000/README.md.
Each middleware module may define command-line options - these will be shown on the usage guide. For example ws --stack lws-static --help
would show
Add your preferred stack to the config file.
// lws.config.js
module.exports = {
stack: [ 'spa', 'static', 'index' ]
}
View built-in middleware list
$ ws middleware-list
[ 'lws-body-parser',
'lws-request-monitor',
'lws-log',
'lws-cors',
'lws-json',
'lws-rewrite',
'lws-blacklist',
'lws-conditional-get',
'lws-mime',
'lws-compress',
'lws-mock-response',
'lws-spa',
'lws-static',
'lws-index' ]