nanohttp is using object-dispatching style to route the application. it can help you to have a pythonic, beautiful, meaningful structure with the best performance.


The requested path will be split-ed by / and python’s getattr will be used on the Root controller recursively to find specific callable to handle request.

from nanohttp import Controller, html

class ChildController(Controller):

    def index(self):
        return '<h1>This is child controller</h1>'

    def name(self):
        return '<h1>children controller</h1>'

    def edit(self, name):
        return '<h1>children edited</h1>'

class Root(Controller):
    children = ChildController()

    def index(self):
        return '<h1>i am root</h1>'

Final path can be like:

/                      -> Root.index()
/children              -> Root.children.index()
/children/name         ->
/children/edit/name    -> Root.children.edit(name)


Remaining url parts will be passed as positional argument to the handler method.

Trailing slashes

If the Controller.__remove_trailing_slash__ is True, then all trailing slashes are ignored.

def test_trailing_slash(self):
        expected_response='User: 10\nAttr: jobs\n'

Rest Controller

For using nanohttp with RESTful rules we have a RestController, it’s behavior same as basic Controller but dispatcher tries to dispatch request using HTTP method(verb) at first.

from nanohttp import RestController, html

class Root(RestController):

    def say_hello(self):
        return '<h1>Hello!</h1>'

    def get(self, some_id: int):
        return '<h1>You called GET Method. (some_id=%s)</h1>' % some_id

    def delete(self):
        return '<h1>You called DELETE Method</h1>'

    def post(self):
        return '<h1>You called POST Method</h1>'

    def patch(self):
        return '<h1>You called PATCH Method</h1>'

Regex Controller

For who want to handle complex URLs, nanohttp also supports Regular Expression dispatcher and here is a easy way to play with:

from nanohttp import RegexRouteController, json

class Root(RegexRouteController):

    def __init__(self):
            ('/user/(?P<installation_id>\d+)/access_tokens', self.access_tokens),

    def access_tokens(self, installation_id: int):
        return dict(