本文最后更新于:2025年1月29日 凌晨
原文地址:Simple implementation principle of Express Middleware
简而言之,express 中间件,就是在服务器端处理请求对象和响应对象的函数。
遵循一个先注册,先执行的原则,通过调用next()
函数,把执行权交给下一个函数。
核心代码:
1 2 3 4 5 6 7
| const next = () => { const stack = stacks.shift(); if(stack) { stack(req, res, next); } } next();
|
express 简易实现的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| const http = require('http') const slice = Array.prototype.slice
class Express { constructor() { this.router = { all: [], get: [], post: [] } }
middlewareHandler(path) { const info = {} if (typeof path === 'string') { info.path = path info.stack = slice.call(arguments, 1) } else { info.path = '/' info.stack = slice.call(arguments, 0) }
return info }
use() { const allStack = this.middlewareHandler(...arguments) this.router.all.push(allStack) }
get() { const getStack = this.middlewareHandler(...arguments) this.router.get.push(getStack) }
post() { const postStack = this.middlewareHandler(...arguments) this.router.post.push(postStack) }
accordStack(method, url) { let stacks = [] stacks = stacks.concat(this.router.all) stacks = stacks.concat(this.router[method]) return stacks .filter(stack => { return url.indexOf(stack.path) !== -1 }).map(item => item.stack[0]) }
handler(req, res, stacks) { const next = () => { const stack = stacks.shift() if(stack) { stack(req, res, next) } } next() }
callback() { return (req, res) => { res.json = data => { res.setHeader('Content-Type', 'application/json') res.end(JSON.stringify(data)) } const {method, url} = req const stacks = this.accordStack(method.toLowerCase(), url) this.handler(req, res, stacks) } }
listen(...args) { const server = http.createServer(this.callback()) server.listen(...args) } }
module.exports = () => { return new Express() }
|