How to create a session middleware?
We create a session middleware for Total.js application in this blog post. It's easy and useful.
How to create a session middleware?
We create a simple middleware
You can save the code below as your-app/definitions/session.js
:
- constant
SESSION
contains all active sessions - constant
TIMEOUT
contains expiration time for a session - constant
COOKIE
contains a cookie name
Usage
Now we can use the middleware, so create/modify a controller e.g. /your-app/controllers/default.js
:
What have we done in the code above?
- we created a route to homepage
F.route('/', action, [flags])
- the route contains
session
middleware, which we created - then we used
controller.session
in the controller's action
Extend middleware by adding e.g. events
Now it's very easy to extend functionality of middleware. So I extend the code below:
Usage:
You can use the code below in each .js
file on the server-side in Total.js.
How to set the middleware to multiple routes together?
F.use()
can sets middleware globally - documentationF.group
creates a group of flags for routes - documentation
Other posts from Total.js Platform
- 2024-11-13Benchmarking Node.js Frameworks: selecting your framework for 2025!
- 2024-11-01October report 2024
- 2024-10-22Performance Testing: Total.js vs. NestJS
- 2024-10-01September report 2024
- 2024-09-27Total.js UI Builder: #2 designing your first interface
- 2024-09-26Total.js V5: Middlewares
- 2024-09-23Beginner Guide to Total.js UI: # 05 Client-side routing
- 2024-09-23Total.js UI #4: Data Binding (Part 2 – Practical Example)
- 2024-09-20Introduction to Total.js UI Builder: A Beginner’s Guide
- 2024-09-13Total.js v5: #06 Understanding File Routing