Search code examples
perldancer2

Pass html file as a route in perl dancer2 MVC framework


I have a file on my machine at /abc/assets/file/

I want to return this file as a route. It is possible to do that ?


Solution

  • You would typically do that outside Dancer in your PSGI file with a different Plack application that's mounted in the same PSGI file. Have a look at Plack::App::File for an individual file, or Plack::App::Directory for an entire directory.

    Your PSGI file would then look something like this.

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    use FindBin;
    use lib "$FindBin::Bin/../lib";
    
    use MyApp; # this is Dancer2-based
    use Plack::Builder;
    use Plack::App::File;
    use Plack::App::Directory;
    
    builder {
        mount '/' => MyApp->to_app;
        mount '/abc-file' => Plack::App::File->new(
            file => '/abc/assets/file')->to_app;
        mount '/foo' => Plack::App::Directory->new({ 
            root => "/xyz/foo" })->to_app;
    };