Middleware is a powerful concept in web development that allows you to intercept and modify requests and responses in your application. In this section, we will explore how to use middleware in your application to enhance functionality and improve the user experience.
Middleware files are located in the /service/middleware directory, and they are automatically loaded by the application.
🔗Create a Middleware
You can create a new middleware by using the newMiddleware macro. Inside the middleware use next() to continue to the next middleware or route handler, and abort() to stop the request and optionally redirect to another page.
Here is an example:
import supranim/middleware
var isAuthenticated = false
newMiddleware authenticate:
# some authentication logic here, if successful, call
next()
# if the auth fails
# redirect to login page if authentication fails
abort("/auth/login")🔗Attach Middleware to Routes
To attach a middleware to a route, you can use a pragma expression. For example to attach the authenticate middleware to /account route:
get "/account" {.middleware: [authenticate].}
# GET route links to `getAccount` controller
# and is protected by the `authenticate` middlewareThis will ensure that the authenticate middleware runs before the route handler for /account, allowing you to perform authentication checks or other logic before processing the request. Sure, you can attach multiple middleware to a route by listing them in the middleware pragma:
get "/account" {.middleware: [authenticate, anotherMiddleware].}🔗Base Middleware
The baseMiddleware is a special middleware that runs before all other middleware and route handlers. It is useful for tasks that need to be performed on every request, such as logging, setting headers, or handling CORS.
One example is to implement a base middleware that checks for a trailing slash in the URI and redirects to the correct URL if necessary:
import supranim/middleware
newBaseMiddleware uriChecker:
## Fix the trailing slash in the URI
let path = req.getUriPath
if path != "/" and path[^1] == '/':
res.addHeader("Location", path[0..^2])
req.resp(code = HttpCode(301), "", res.getHeaders())In this example, the uriChecker middleware checks if the requested URI ends with a trailing slash (except for the root path "/"). If it does, it redirects the user to the same URI without the trailing slash using a 301 Moved Permanently response.
Base middleware handlers are automatically added to each request, so you don't need to manually include them in your routes. They will run before any other middleware or route handlers, ensuring that the necessary checks or modifications are applied to every request.