The App
is the main point of the application.
It acts as a Router
that holds the top level route handlers.
Here an example to create a simple web app with Popcorn:
import popcorn
class HelloHandler
super Handler
redef fun get(req, res) do res.html "<h1>Hello World!</h1>"
end
var app = new App
app.use("/", new HelloHandler)
The Popcorn app listens on port 3000 for connections.
The app responds with "Hello World!" for request to the root URL (/
) or route.
For every other path, it will respond with a 404 Not Found.
The req
(request) and res
(response) parameters are the same that nitcorn provides
so you can do anything else you would do in your route without Popcorn involved.
Run the app with the following command:
nitc app.nit && ./app
Then, load http://localhost:3000 in a browser to see the output.
popcorn :: App :: defaultinit
HttpResponse
destined to the client in response to the request
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
popcorn :: App :: defaultinit
nitcorn :: Action :: defaultinit
core :: Object :: defaultinit
popcorn :: Router :: defaultinit
popcorn :: Handler :: defaultinit
popcorn :: Handler :: deserialize_body
Deserialize the request bodycore :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).nitcorn :: Action :: prepare_respond_and_close
Full to arequest
with sending the response and closing of the http_server
popcorn :: Router :: use_before
Register a pre-handler for a routepath
.
popcorn :: Handler :: validate_body
Validate body input withvalidator
popcorn :: Handler :: validator
Validator used to check body inputpopcorn :: Handler :: validator=
Validator used to check body input
# Popcorn application.
#
# The `App` is the main point of the application.
# It acts as a `Router` that holds the top level route handlers.
#
# Here an example to create a simple web app with Popcorn:
#
# ~~~
# import popcorn
#
# class HelloHandler
# super Handler
#
# redef fun get(req, res) do res.html "<h1>Hello World!</h1>"
# end
#
# var app = new App
# app.use("/", new HelloHandler)
# # app.listen("localhost", 3000)
# ~~~
#
# The Popcorn app listens on port 3000 for connections.
# The app responds with "Hello World!" for request to the root URL (`/`) or **route**.
# For every other path, it will respond with a **404 Not Found**.
#
# The `req` (request) and `res` (response) parameters are the same that nitcorn provides
# so you can do anything else you would do in your route without Popcorn involved.
#
# Run the app with the following command:
#
# ~~~bash
# nitc app.nit && ./app
# ~~~
#
# Then, load [http://localhost:3000](http://localhost:3000) in a browser to see the output.
class App
super Router
end
lib/popcorn/pop_handlers.nit:383,1--420,3
# App acts like a wrapper around a nitcorn `Action`.
redef class App
super Action
# Do not show anything on console
var quiet = false is writable
# Start listening on `host:port`.
fun listen(host: String, port: Int) do
var iface = "{host}:{port}"
var vh = new VirtualHost(iface)
vh.routes.add new Route("/", self)
var fac = new HttpFactory.and_libevent
fac.config.virtual_hosts.add vh
if not quiet then
print "Launching server on http://{iface}/"
end
fac.run
end
# Handle request from nitcorn
redef fun answer(req, uri) do
uri = uri.simplify_path
var res = new HttpResponse(404)
for route, handler in pre_handlers do
handler.handle(route, uri, req, res)
end
for route, handler in handlers do
handler.handle(route, uri, req, res)
if res.sent then break
end
if not res.sent then
res.send(error_tpl(res.status_code, res.status_message), 404)
end
for route, handler in post_handlers do
handler.handle(route, uri, req, res)
end
res.session = req.session
return res
end
#
fun error_tpl(status: Int, message: nullable String): Template do
return new ErrorTpl(status, message)
end
end
lib/popcorn/popcorn.nit:26,1--74,3