53. 实现middleware

JavaScript

中等难度  -通过 / -执行

你是否使用过 Express Middleware ?

Middleware函数是指可以串联起来的,有特定interface的函数。

app.use('/user/:id', function (req, res, next) {  next()}, function (req, res, next) {  next(new Error('sth wrong'))})

请实现一个简单的middleware 系统!

type Request = objecttype NextFunc =  (error?: any) => voidtype MiddlewareFunc =   (req: Request, next: NextFunc) => voidtype ErrorHandler =   (error: Error, req: Request, next: NextFunc) => voidclass Middleware {  use(func: MiddlewareFunc | ErrorHandler) {    // do any async operations    // call next() to trigger next function  }  start(req: Request) {    // trigger all functions with a req object  }}

有了上述之后,我们可以支持类似Express的用法。

const middleware = new Middleware()middleware.use((req, next) => {   req.a = 1   next()})middleware.use((req, next) => {   req.b = 2   next()})middleware.use((req, next) => {   console.log(req)})middleware.start({})// {a: 1, b: 2}

注意到 use() 支持ErrorHandler,ErrorHandler有3个参数,在发生Error或者next()被含参数调用的时候触发,比如这样。

const middleware = new Middleware()// throw an error at first functionmiddleware.use((req, next) => {   req.a = 1   throw new Error('sth wrong')    // or `next(new Error('sth wrong'))`})// since error occurs, this is skippedmiddleware.use((req, next) => {   req.b = 2})// since error occurs, this is skippedmiddleware.use((req, next) => {   console.log(req)})// since error occurs, this is calledmiddleware.use((error, req, next) => {   console.log(error)   console.log(req)})middleware.start({})// Error: sth wrong// {a: 1}

始终思考更好的解决办法