Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Serving static files with Plack

by bangor (Monk)
on Apr 20, 2016 at 21:08 UTC ( [id://1161026]=perlquestion: print w/replies, xml ) Need Help??

bangor has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a project and using Plack for the first time. I am having a little problem getting my static files served...

Directory structure:
app/test.psgi app/pod-files/test.pod app/static/test.html app/html-files/test.html
The code I have for the builder part in test.psgi is:
builder { enable "Static", path => qr!^/static!, ; enable "Plack::Middleware::Pod", # great module! path => qr!^/pod!, root => 'pod-files', pod_view => 'Pod::POM::View::HTML', ; $app; };
The pod and static files serve up fine:
/app/pod/test.pod OK /app/static/test.html OK
But I want to serve the static files from a different directory to static, so I change the builder to:
builder { enable "Static", path => qr!^/static!, root => 'html-files', ; enable "Plack::Middleware::Pod", path => qr!^/pod!, root => 'pod-files', pod_view => 'Pod::POM::View::HTML', ; $app; };
But, this doesnt work - I now get:
/app/pod/test.pod OK /app/static/test.html not found
I have played around with different values of 'root' but no luck. Any help appreciated.

** Update: managed to work it out - see my comment below

Replies are listed 'Best First'.
Re: Serving static files with Plack
by bangor (Monk) on Apr 20, 2016 at 23:05 UTC
    After a little prod from anon monk I read the docs a little closer and found my answer in Plack::App::File
    enable "Static", path => sub { s!^/static!! }, root => './html-files', ;
    My mistake was thinking PM::Static behaved the same as PM::Pod - when i looked at the source of both i saw that PM::Pod was doing the substitution itself:
    Plack::Middleware::Pod my $r = $self->root || './'; $path =~ s!$path_match!$r!;
Re: Serving static files with Plack
by Anonymous Monk on Apr 20, 2016 at 21:53 UTC
    What documentation are you reading?

        Try  path => sub { s{^/static/}{}; }, root => 'html-files';

        When you only add root, its looking in root/static,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1161026]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2026-03-16 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.