The JsWebApp Object
The core JsWebApp
class that ties everything together. It's responsible for routing, configuration, middleware, and handling the WSGI application lifecycle.
Initialization
You create an instance of JsWebApp
by passing a configuration object.
# app.py
import config
from jsweb.app import JsWebApp
app = JsWebApp(config=config)
Routing
The @app.route()
decorator is used to associate a view function with a URL path and HTTP methods.
@app.route('/')
def home(req, res):
return "Hello, World!"
@app.route('/users/<int:user_id>', methods=['GET', 'POST'])
def user_profile(req, res, user_id):
# ...
Registering Blueprints
Use the register_blueprint()
method to mount a blueprint onto the application.
from my_module.views import my_blueprint
app.register_blueprint(my_blueprint)
Template Filters
You can add custom filters to the Jinja2 template environment using the @app.filter()
decorator.
@app.filter('uppercase')
def to_uppercase(value):
return str(value).upper()
In your template:
{{ my_variable|uppercase }}
Middleware
JsWeb automatically applies a chain of middleware to every request. The order is important:
CSRFMiddleware
: Protects against Cross-Site Request Forgery.StaticFilesMiddleware
: Serves static files from the configured directory.DBSessionMiddleware
: Manages the SQLAlchemy database session lifecycle.
This is handled internally within the __call__
method of the JsWebApp
instance.