1Plack::Middleware::StatUisce(r3)Contributed Perl DocumenPtlaatciko:n:Middleware::Static(3)
2
3
4
6 Plack::Middleware::Static - serve static files with Plack
7
9 use Plack::Builder;
10
11 builder {
12 enable "Plack::Middleware::Static",
13 path => qr{^/(images|js|css)/}, root => './htdocs/';
14 $app;
15 };
16
18 This middleware allows your Plack-based application to serve static
19 files.
20
21 Note that if you are building an app using Plack::App::URLMap, you
22 should consider using Plack::App::File to serve static files instead.
23 This makes the overall routing of your application simpler to
24 understand.
25
26 With this middleware, if a static file exists for the requested path,
27 it will be served. If it does not exist, by default this middleware
28 returns a 404, but you can set the "pass_through" option to change this
29 behavior.
30
31 If the requested document is not within the "root" or the file is there
32 but not readable, this middleware will return a 403 Forbidden response.
33
34 The content type returned will be determined from the file extension by
35 using Plack::MIME or using "content_type".
36
38 path, root
39 enable "Plack::Middleware::Static",
40 path => qr{^/static/}, root => 'htdocs/';
41
42 The "path" option specifies the URL pattern (regular expression) or
43 a callback to match against requests. If the <path> option matches,
44 the middleware looks in "root" to find the static files to serve.
45 The default value of "root" is the current directory.
46
47 This example configuration serves "/static/foo.jpg" from
48 "htdocs/static/foo.jpg". Note that the matched portion of the path,
49 "/static/", still appears in the locally mapped path under "root".
50 If you don't want this to happen, you can use a callback to munge
51 the path as you match it:
52
53 enable "Plack::Middleware::Static",
54 path => sub { s!^/static/!! }, root => 'static-files/';
55
56 The callback should operate on $_ and return a true or false value.
57 Any changes it makes to $_ are used when looking for the static
58 file in the "root".
59
60 The configuration above serves "/static/foo.png" from
61 "static-files/foo.png", not "static-files/static/foo.png". The
62 callback specified in the "path" option matches against $_ munges
63 this value using "s///". The substitution operator returns the
64 number of matches it made, so it will return true when the path
65 matches "^/static".
66
67 For more complex static handling in the "path" callback, in
68 addition to $_ being set the callback receives two arguments,
69 "PATH_INFO" (same as $_) and $env.
70
71 If you want to map multiple static directories from different
72 roots, simply add this middleware multiple times with different
73 configuration options.
74
75 pass_through
76 When this option is set to a true value, then this middleware will
77 never return a 404 if it cannot find a matching file. Instead, it
78 will simply pass the request on to the application it is wrapping.
79
80 content_type
81 The "content_type" option can be used to provide access to a
82 different MIME database than Plack::MIME. Plack::MIME works fast
83 and good for a list of well known file endings, but if you need a
84 more accurate content based checking you can use modules like
85 File::MimeInfo or File::MMagic for example. The callback should
86 work on $_[0] which is the filename of the file.
87
89 Tokuhiro Matsuno, Tatsuhiko Miyagawa
90
92 Plack::Middleware Plack::Builder
93
94
95
96perl v5.30.0 2019-07-26 Plack::Middleware::Static(3)