Skip to content

Commit

Permalink
Merge pull request reactjs#90 from dnmfarrell/master
Browse files Browse the repository at this point in the history
Tutorial server in Perl, with example start code
  • Loading branch information
zpao committed Oct 26, 2015
2 parents 66ccecd + 208e00f commit 55e01a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ go get github.com/xyproto/algernon
algernon server.lua
```

### Perl

```sh
cpan Mojolicious
perl server.pl
```

And visit <http://localhost:3000/>. Try opening multiple tabs!

## Changing the port
Expand Down
34 changes: 34 additions & 0 deletions server.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This file provided by Facebook is for non-commercial testing and evaluation
# purposes only. Facebook reserves all rights not expressly granted.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use Mojolicious::Lite;
use Mojo::JSON qw(encode_json decode_json);

app->static->paths->[0] = './public';

any '/' => sub { $_[0]->reply->static('index.html') };

any [qw(GET POST)] => '/api/comments' => sub {
my $self = shift;
my $comments = decode_json (do { local(@ARGV,$/) = 'comments.json';<> });

if ($self->req->method eq 'POST')
{
push @$comments, {
author => $self->param('author'),
text => $self->param('text'),
};
open my $FILE, '>', 'comments.json';
print $FILE encode_json($comments);
}
$self->render(json => $comments);
};
my $port = $ENV{PORT} || 3000;
app->start('daemon', '-l', "http://*:$port");

0 comments on commit 55e01a9

Please sign in to comment.