项目作者: JWebCoder

项目描述 :
A middleware framework for Deno's http/s server.
高级语言: TypeScript
项目地址: git://github.com/JWebCoder/mith.git
创建时间: 2020-05-12T10:14:56Z
项目社区:https://github.com/JWebCoder/mith

开源协议:MIT License

下载


Mith

mith ci
deno doc

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

Available middlewares

  • mith_router - router middleware
  • cookieSession.ts - session middleware using cookies
  • mith_static - static file server

Note: These middlewares will eventually move to their own repositories to split responsibilities and improve maintenance

Usage

Basic integration

  1. import { Mith, NextFunction, Request, Response } from 'https://deno.land/x/mith@v0.7.0/mod.ts'
  2. const app = new Mith()
  3. app.before(
  4. async (req: Request, res: Response, next: NextFunction) => {
  5. res.body.before = true
  6. next()
  7. }
  8. )
  9. app.after(
  10. (req: Request, res: Response, next: NextFunction) => {
  11. const encoder = new TextEncoder();
  12. Deno.writeFile('./after.dat', encoder.encode(`${Deno.pid} - ${JSON.stringify(res.body)}\n`), {append: true})
  13. next()
  14. }
  15. )
  16. app.use(
  17. async (req: Request, res: Response, next: NextFunction) => {
  18. const body = await req.body()
  19. switch (body.type) {
  20. case 'error':
  21. return next(new Error('error'))
  22. case 'redirect':
  23. res.redirect('/')
  24. break
  25. case 'urlencoded':
  26. case 'json':
  27. res.body.test = body.type
  28. break
  29. default:
  30. res.body.test = body
  31. }
  32. next()
  33. }
  34. )
  35. app.error(
  36. (req: Request, res: Response, next: NextFunction) => {
  37. res.status = res.error.status || 500
  38. res.body = res.error.message
  39. next()
  40. }
  41. )
  42. export default app

Right now I’m still working on the documentation, so you can check the example folder for full usage examples

Multiple stacks

image

app.before()

Intended to be used by middleware the enhances the request or response objects. Multipart body parser for example

app.main()

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

app.error()

This middleware stack will be triggered if the callback function next is called with an error: next(something)

app.after()

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.

Middleware parameters

Request

The request contains information about the request received

properties:

  • body()
    Parses the body of the request and returns it in json format
  • query()
    Parses the query string of the request and returns an URLSearchParams object
  • serverRequest
    The original Deno server request

Response

The response contains information about the response that will be sent back to the requestor.

properties:

  • error
    Contains the error sent when calling next(error)
  • body
    The body of the response
  • headers
    A Headers instance which contains the headers for the response
  • finished
    A boolean indicating that the response is completed
  • sent
    A boolean indicating that the response has been already sent to the requestor
  • send
    Sends the response to the requestor
  • redirect
    Redirects the user to another location

Next

A function that triggers the next middleware in line.

Triggers the next middleware

  1. next()

Jumps to the error middleware stack

  1. next([someinput])

On the error middleware stack calling next([someinput]) has no effect because connection is already on the error stack