Re: Simple webserver - serve STATIC files with Mojolicious

http://mojolicio.us/perldoc/Mojolicious/Static is API documentation; unless you're mucking about the core, you want to be looking at the guides instead.

In a lite app:

push @{app->static->paths} => 'some directory';


In a full app, in startup:

push @{$self->static->paths} => 'some directory';


Alright, DUDE!
Thanks for getting me up and running with paths to static files. Also, thank you for advising me to return to the basic guides. At first I did a google search for the code you suggested ( push @{app... ) which brought me to yet another API doc fragment http://mojolicio.us/perldoc/Mojolicious#static
There are so many references to $app in all the API docs, but it was only in this one that I saw:


my $app = Mojolicious->new;


and from there I thought I would be able to apply the code you shared. But NO, that just broke my server. Luckily within that doc was a link to http://mojolicio.us/perldoc/Mojolicious/Guides (back to basics, right) and then on to http://mojolicio.us/perldoc/Mojolicious/Lite#app where I finally figure out: app is a SUBROUTINE (without $ and maybe only in Mojolicious::Lite)

And then the real breakthrough moment came when I looked at http://mojolicio.us/perldoc/Mojolicious/Lite#app:


my $app = app;


OHHH! Now I'm able to put it all together with what you shared.
So, to add a custom path from which static files can be served by a Mojolicious::Lite application:

#!/usr/bin/env perl
use Mojolicious::Lite;

my $app = app;
my $static = $app->static;
push @{$static->paths}, ($ENV{PWD});

get '/' => sub {
    my $self = shift;
    #... do something for a homepage here ...
};

$app->start;

AWESOME! Now I can stick any file or subdirectory+file in my project's root directory and request it from the server with:

localhost:3000/somefile

or
localhost:3000/subdirectory/somefile

WooHOO!

THANK YOU!

Rev

Leave a comment

About Revlin John

user-pic Perl, Mojolicious and MongoDB are opening up a whole new world of integrative web application development for me. I would like to share my experiences with these technologies and learn from others :)