Documentation
Middleware
LiteNode introduces comprehensive middleware support, both globally and at the route level. Developers can easily add middleware functions to handle cross-cutting concerns such as authentication, logging, and request processing. The ability to apply middleware globally, as well as to individual routes, merge, and nest operations, provides greater flexibility and control over request processing in LiteNode.
use
Signature:
use(middleware: RouteHandler): LiteNode
Adds a middleware function to the middleware stack.
middleware
: The middleware function to add.- Returns: A reference to the LiteNode instance for method chaining.
Example:
// Define a middleware function to log incoming requests globally
app.use(async (req, res) => {
// Log the method and URL of the incoming request
console.log(`${req.method} ${req.url}`)
})
Middleware
Signature:
type Middleware = RouteHandler
Adds a middleware function to the middleware stack
middleware
: The middleware function to add.- Returns: A reference to the LiteNode instance for method chaining.
Example:
// Middleware function to add a timestamp to the request object
const addTimestamp = async (req, res) => {
// Add a timestamp to the request object
req.timestamp = new Date()
}
// Add route with middleware
app.get("/route-with-timestamp-middleware", addTimestamp, async (req, res) => {
// Retrieve the timestamp from the request object
const timestamp = req.timestamp
// Send a response with the timestamp
res.end(`Route with a middleware function. Request timestamp: ${timestamp}`)
})