Skip to content
This repository
Browse code

Improve static file handler guide chapter

Add more infos about MIME types and the file option.
  • Loading branch information...
commit a2f4703e5ef01642870d503ef36a38847387417a 1 parent beaae7b
Loïc Hoguin authored
32  guide/static_handlers.md
Source Rendered
@@ -19,8 +19,7 @@ Static handlers are pre-written REST handlers. They only need
19 19
 to be specified in the routing information with the proper options.
20 20
 
21 21
 The following example routing serves all files found in the
22  
-`priv_dir/static/` directory of the application `my_app`. It uses
23  
-a mimetypes library to figure out the files' content types.
  22
+`priv_dir/static/` directory of the application `my_app`.
24 23
 
25 24
 ``` erlang
26 25
 Dispatch = [
@@ -32,3 +31,32 @@ Dispatch = [
32 31
 	]}
33 32
 ].
34 33
 ```
  34
+
  35
+You can also serve a single file specifically. A common example
  36
+would be an `index.html` file to be served when the path `/`
  37
+is requested. The following example will serve the `priv/index.html`
  38
+file from the application `my_app`.
  39
+
  40
+``` erlang
  41
+Dispatch = [
  42
+	{'_', [
  43
+		{"/", cowboy_static, [
  44
+			{directory, {priv_dir, my_app, []}},
  45
+			{file, "index.html"},
  46
+			{mimetypes, {fun mimetypes:path_to_mimes/2, default}}
  47
+		]}
  48
+	]}
  49
+].
  50
+```
  51
+
  52
+MIME type
  53
+---------
  54
+
  55
+Cowboy does not provide any default for MIME types. This means
  56
+that unless you specify the `mimetypes` option, all files will
  57
+be sent as `application/octet-stream`, which the browser will
  58
+not try to interpret, instead trying to make you download it.
  59
+
  60
+In the examples above we used the
  61
+[mimetypes application](https://github.com/spawngrid/mimetypes)
  62
+to find the MIME type from the file's extension.
1  guide/toc.md
Source Rendered
@@ -34,6 +34,7 @@ Cowboy User Guide
34 34
  *  [Static handlers](static_handlers.md)
35 35
    *  Purpose
36 36
    *  Usage
  37
+   *  MIME type
37 38
  *  [Request object](req.md)
38 39
    *  Purpose
39 40
    *  Request

0 notes on commit a2f4703

Please sign in to comment.
Something went wrong with that request. Please try again.