File Router
The FileRouter is a filesystem-based routing system for Nexios that automatically discovers and registers route handlers from Python files in a specified directory structure. This system follows convention-over-configuration principles, making API route management intuitive and scalable while reducing boilerplate code.
Core Features
Automatic Route Registration: Scans directories for route files and maps them to URL paths
HTTP Method Detection: Recognizes standard HTTP methods (GET, POST, PUT, etc.) from handler function names
Dynamic Path Parameters: Supports parameterized routes using curly brace syntax
Metadata Support: Enables OpenAPI documentation through decorators
Flexible Configuration: Allows exclusion of specific directories from scanning
Installation and Setup
To use the FileRouter, first ensure Nexios is installed, then configure the router in your application entry point:
Configuration Options
The FileRouter accepts a configuration dictionary with the following properties:
root (str): Required. The base directory to scan for route files
exempt_paths (list[str]): Optional. List of directory names to exclude from scanning
Example with custom configuration:
Filesystem Routing Conventions
The router maps directory structure to URL paths following these rules:
Each directory represents a path segment
Files must be named
route.py
to be detectedDynamic parameters are specified using curly braces
{param}
Example Structure
Route Handlers
Basic Route Handling
For simple routes, define functions named after HTTP methods in your route.py
files:
Accessing Path Parameters
Dynamic route parameters are available in the request object:
Advanced Route Configuration
For more control over route behavior and documentation, use the @mark_as_route
decorator:
Available Decorator Options
path
Custom URL path (overrides auto-detected path)
methods
List of HTTP methods (defaults to function name)
summary
Brief description for OpenAPI docs
description
Detailed route description
request_model
Pydantic model for request validation
responses
Dictionary mapping status codes to response descriptions
tags
OpenAPI tags for grouping related routes
security
Security requirements for the route
middlewares
Route-specific middleware functions
operation_id
Unique identifier for the operation in OpenAPI docs
deprecated
Marks the route as deprecated in documentation
parameters
Additional OpenAPI parameter definitions
Error Handling
The FileRouter integrates with Nexios's exception handling system. Define custom error handlers in your application:
Limitations
Route files must be named
route.py
Directory scanning occurs at startup (changes require restart in production)
Complex routing patterns may require manual route registration
Example Project Structure
Template Rendering System
The html.py
module provides Jinja2 template rendering capabilities for Nexios applications. This system integrates seamlessly with the FileRouter to enable server-side HTML rendering.
Core Components
Template Environment Configuration
Global Jinja2 environment management
Custom loader implementation
Auto-escaping and auto-reloading support
Rendering Decorator
@render()
decorator for route handlersAutomatic template discovery
Context passing from handlers to templates
Template Configuration
Global Setup
Configure the template system at application startup:
Configuration Options
template_dir
: Base directory for template filesenv
: Pre-configured Jinja2 environment (overrides template_dir)**env_options
: Additional Jinja2 environment parameters
Template Rendering in Routes
Basic Usage
Template Directory Structure
Recommended structure for templates:
Advanced Features
Template Inheritance
Custom Loader Behavior
The included Loader
class provides:
Template file discovery
Modification time checking
Automatic reloading in development
Context Validation
For type-safe templates, use Pydantic models:
Error Handling
The rendering system provides clear error messages for:
Missing templates
Invalid context data
Template syntax errors
Performance Considerations
Template Caching: Enabled by default in production
Auto-reload: Disable in production for better performance
Pre-compilation: Consider pre-compiling templates for deployment
Debugging Tips
Set
auto_reload=True
during developmentUse
print(template_directory)
in the render decorator to verify pathsCheck Jinja2's built-in debugging features
Complete Example
This system provides a robust, type-safe way to render HTML templates while maintaining clean separation of concerns in your Nexios application.
Last updated