Middlewares
Nestipy define middleware like NestJs do.
Python
from nestipy.common import Injectable
from nestipy.common import Request, Response
from nestipy.common import NestipyMiddleware
from nestipy.types_ import NextFn
@Injectable()
class LoggerMiddleware(NestipyMiddleware):
async def use(self, req: Request, res: Response, next_fn: NextFn):
print('Requesting ....')
await next_fn()
Dependency injection
Nestipy middleware support dependency injection
Applying middleware
Nestipy apply middleware like the way Nestjs use. Modules that include middleware have to implement the NestipyModule.
app_module.py
from nestipy.dynamic_module import NestipyModule
from nestipy.common import Module
from nestipy.core import MiddlewareConsumer
@Module()
class AppModule(NestipyModule):
def configure(self, consumer: MiddlewareConsumer):
consumer.apply(LoggerMiddleware).for_route('cats')
Apply middleware for controller
We can apply middleware for controller and excludes some routes.
app_module.py
from nestipy.dynamic_module import NestipyModule
from nestipy.common import Module
from nestipy.core import MiddlewareConsumer
@Module()
class AppModule(NestipyModule):
def configure(self, consumer: MiddlewareConsumer):
consumer.apply(LoggerMiddleware).for_route(CatsController).excludes([])
Functional middleware
Python
from nestipy.common import Request, Response
from nestipy.types_ import NextFn
async def logger(req: Request, res: Response, next_fn: NextFn):
print('Requesting ....')
await next_fn()
And use it within the AppModule:
Global middleware
Python
from nestipy.core import NestipyFactory
app = NestipyFactory.create(AppModule)
app.use(logger)
Take a look here for an example.
Support us
Nestipy is a project released under the MIT license, meaning it's open source and freely available for use and modification. Its development thrives with the generous contributions of these fantastic individuals.