A middleware framework for Deno's http/s server.
A middleware framework for Deno’s http/s server
Highly inspired by the Express middleware system
Differently from Express, the main Mith application is only responsible to handle the middleware system, it has no routes support, and it tries to leave the request and response objects form Deno untouched as much as possible
Note: These middlewares will eventually move to their own repositories to split responsibilities and improve maintenance
Basic integration
import { Mith, NextFunction, Request, Response } from 'https://deno.land/x/mith@v0.7.0/mod.ts'
const app = new Mith()
app.before(
async (req: Request, res: Response, next: NextFunction) => {
res.body.before = true
next()
}
)
app.after(
(req: Request, res: Response, next: NextFunction) => {
const encoder = new TextEncoder();
Deno.writeFile('./after.dat', encoder.encode(`${Deno.pid} - ${JSON.stringify(res.body)}\n`), {append: true})
next()
}
)
app.use(
async (req: Request, res: Response, next: NextFunction) => {
const body = await req.body()
switch (body.type) {
case 'error':
return next(new Error('error'))
case 'redirect':
res.redirect('/')
break
case 'urlencoded':
case 'json':
res.body.test = body.type
break
default:
res.body.test = body
}
next()
}
)
app.error(
(req: Request, res: Response, next: NextFunction) => {
res.status = res.error.status || 500
res.body = res.error.message
next()
}
)
export default app
Right now I’m still working on the documentation, so you can check the example folder for full usage examples
Intended to be used by middleware the enhances the request or response objects. Multipart body parser for example
Main business logic of the application goes here, routing definitions for example
Using app.use()
will default to the main stack if no stack is passed
This middleware stack will be triggered if the callback function next is called with an error: next(something)
This middleware stack runs after sending the response to the user, it’s intended to be used with extra integrations that are not directly related to the user response. Analytics or logging for example.
The request contains information about the request received
The response contains information about the response that will be sent back to the requestor.
A function that triggers the next middleware in line.
Triggers the next middleware
next()
Jumps to the error middleware stack
next([someinput])
On the error middleware stack calling next([someinput]) has no effect because connection is already on the error stack