Plack Apps in Javascript

Love Plack web apps but feel like writing some Javascript today instead of Perl?

# app.psgi
use Plack::App::JSP;
Plack::App::JSP->new( js => q{
[ 200, [ 'Content-type', 'text/html' ], [ 'Hello, World!' ] ] 
});

# displays: Hello, World!

Given the similarities between Perl and JSON you can’t actually tell if I’m cheating or not from the above snippet.

Let’s try something more convincing:

Plack::App::JSP->new( js => q{
function respond(body) {
    return [ 200, [ 'Content-type', 'text/html' ], [ body ] ]
}

respond("Five factorial is " +
    (function(x) {
      if ( x<2 ) return 1;
      return x * arguments.callee(x - 1);
    })(5)
);
});

# displays: Five factorial is 120

I just pushed Plack::App::JSP to the CPAN.
Thanks to Salvador Ortiz and Miguel Ibarra whose recently released JSP module makes this possible.

3 thoughts on “Plack Apps in Javascript”

  1. You’ve a bug in your factorial function. It obviously doesn’t matter for your example but 0! is 1, not 0. So …

    if (x < 2) return 1;

    Though, of course factorial of negative numbers is undefined.

Comments are closed.