mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-16 13:10:24 +00:00
Improve static file handler guide chapter
Add more infos about MIME types and the file option.
This commit is contained in:
parent
beaae7bf70
commit
a2f4703e5e
2 changed files with 31 additions and 2 deletions
|
@ -19,8 +19,7 @@ Static handlers are pre-written REST handlers. They only need
|
||||||
to be specified in the routing information with the proper options.
|
to be specified in the routing information with the proper options.
|
||||||
|
|
||||||
The following example routing serves all files found in the
|
The following example routing serves all files found in the
|
||||||
`priv_dir/static/` directory of the application `my_app`. It uses
|
`priv_dir/static/` directory of the application `my_app`.
|
||||||
a mimetypes library to figure out the files' content types.
|
|
||||||
|
|
||||||
``` erlang
|
``` erlang
|
||||||
Dispatch = [
|
Dispatch = [
|
||||||
|
@ -32,3 +31,32 @@ Dispatch = [
|
||||||
]}
|
]}
|
||||||
].
|
].
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also serve a single file specifically. A common example
|
||||||
|
would be an `index.html` file to be served when the path `/`
|
||||||
|
is requested. The following example will serve the `priv/index.html`
|
||||||
|
file from the application `my_app`.
|
||||||
|
|
||||||
|
``` erlang
|
||||||
|
Dispatch = [
|
||||||
|
{'_', [
|
||||||
|
{"/", cowboy_static, [
|
||||||
|
{directory, {priv_dir, my_app, []}},
|
||||||
|
{file, "index.html"},
|
||||||
|
{mimetypes, {fun mimetypes:path_to_mimes/2, default}}
|
||||||
|
]}
|
||||||
|
]}
|
||||||
|
].
|
||||||
|
```
|
||||||
|
|
||||||
|
MIME type
|
||||||
|
---------
|
||||||
|
|
||||||
|
Cowboy does not provide any default for MIME types. This means
|
||||||
|
that unless you specify the `mimetypes` option, all files will
|
||||||
|
be sent as `application/octet-stream`, which the browser will
|
||||||
|
not try to interpret, instead trying to make you download it.
|
||||||
|
|
||||||
|
In the examples above we used the
|
||||||
|
[mimetypes application](https://github.com/spawngrid/mimetypes)
|
||||||
|
to find the MIME type from the file's extension.
|
||||||
|
|
|
@ -34,6 +34,7 @@ Cowboy User Guide
|
||||||
* [Static handlers](static_handlers.md)
|
* [Static handlers](static_handlers.md)
|
||||||
* Purpose
|
* Purpose
|
||||||
* Usage
|
* Usage
|
||||||
|
* MIME type
|
||||||
* [Request object](req.md)
|
* [Request object](req.md)
|
||||||
* Purpose
|
* Purpose
|
||||||
* Request
|
* Request
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue