aboutsummaryrefslogtreecommitdiffstats
path: root/guide/static_handlers.md
blob: d6347f09da8abbf96ab01343de48d8c8acc8865b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Static handlers
===============

Purpose
-------

Static handlers are a built-in REST handler for serving files. They
are available as a convenience and provide fast file serving with
proper cache handling.

It is recommended to use a Content Distribution Network (CDN) or at
least a dedicated file server running on a dedicated cookie-less
hostname for serving your application's static files in production.

Usage
-----

Static handlers are pre-written REST handlers. They only need
to be specified in the routing information with the proper options.

The following example routing serves all files found in the
`priv_dir/static/` directory of the application `my_app`.

``` erlang
Dispatch = [
	{'_', [
		{"/[...]", cowboy_static, [
			{directory, {priv_dir, my_app, [<<"static">>]}},
			{mimetypes, {fun mimetypes:path_to_mimes/2, default}}
		]}
	]}
].
```

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.