1Mojo::Server(3) User Contributed Perl Documentation Mojo::Server(3)
2
3
4
6 Mojo::Server - HTTP/WebSocket server base class
7
9 package Mojo::Server::MyServer;
10 use Mojo::Base 'Mojo::Server';
11
12 sub run {
13 my $self = shift;
14
15 # Get a transaction
16 my $tx = $self->build_tx;
17
18 # Emit "request" event
19 $self->emit(request => $tx);
20 }
21
23 Mojo::Server is an abstract base class for HTTP/WebSocket servers and
24 server interfaces, like Mojo::Server::CGI, Mojo::Server::Daemon,
25 Mojo::Server::Hypnotoad, Mojo::Server::Morbo, Mojo::Server::Prefork and
26 Mojo::Server::PSGI.
27
29 Mojo::Server inherits all events from Mojo::EventEmitter and can emit
30 the following new ones.
31
32 request
33 $server->on(request => sub {
34 my ($server, $tx) = @_;
35 ...
36 });
37
38 Emitted when a request is ready and needs to be handled.
39
40 $server->on(request => sub {
41 my ($server, $tx) = @_;
42 $tx->res->code(200);
43 $tx->res->headers->content_type('text/plain');
44 $tx->res->body('Hello World!');
45 $tx->resume;
46 });
47
49 Mojo::Server implements the following attributes.
50
51 app
52 my $app = $server->app;
53 $server = $server->app(MojoSubclass->new);
54
55 Application this server handles, defaults to a Mojo::HelloWorld object.
56
57 reverse_proxy
58 my $bool = $server->reverse_proxy;
59 $server = $server->reverse_proxy($bool);
60
61 This server operates behind a reverse proxy, defaults to the value of
62 the "MOJO_REVERSE_PROXY" environment variable.
63
65 Mojo::Server inherits all methods from Mojo::EventEmitter and
66 implements the following new ones.
67
68 build_app
69 my $app = $server->build_app('MyApp');
70 my $app = $server->build_app('MyApp', log => Mojo::Log->new);
71 my $app = $server->build_app('MyApp', {log => Mojo::Log->new});
72
73 Build application from class and assign it to "app".
74
75 build_tx
76 my $tx = $server->build_tx;
77
78 Let application build a transaction.
79
80 daemonize
81 $server->daemonize;
82
83 Daemonize server process.
84
85 load_app
86 my $app = $server->load_app('/home/sri/myapp.pl');
87
88 Load application from script and assign it to "app".
89
90 say Mojo::Server->new->load_app('./myapp.pl')->home;
91
92 new
93 my $server = Mojo::Server->new;
94 my $server = Mojo::Server->new(reverse_proxy => 1);
95 my $server = Mojo::Server->new({reverse_proxy => 1});
96
97 Construct a new Mojo::Server object and subscribe to "request" event
98 with default request handling.
99
100 run
101 $server->run;
102
103 Run server. Meant to be overloaded in a subclass.
104
106 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
107
108
109
110perl v5.32.0 2020-07-28 Mojo::Server(3)