From 41e4f9de6d563a024545179775e64ef20ff77e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Wed, 23 Sep 2020 19:03:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B7=AF=E7=94=B1;=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 15 ++++++++++----- middleware/router.js | 5 +++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 259e328..b023f98 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ export default class Five { constructor() { hideProperty(this, '__FIVE__', Object.assign({}, init)) hideProperty(this, '__MODULES__', {}) - hideProperty(this, '__MIDDLEWARE__', []) + hideProperty(this, '__MIDDLEWARE__', [credentialsWare]) } __init__() { @@ -64,7 +64,7 @@ export default class Five { // 将session和cookie的中间件提到最前 // 以便用户自定义的中间件可以直接操作session和cookie // this.__MIDDLEWARE__.unshift(sessionWare) - this.__MIDDLEWARE__.unshift(credentialsWare) + // this.__MIDDLEWARE__.unshift(credentialsWare) this.use(routerWare) } @@ -148,13 +148,18 @@ export default class Five { this.__init__() server = http.createServer(function(req, res) { - var response = new Response(req, res) var request = new Request(req, res) - - response.set('X-Powered-By', 'Five.js') + var response = new Response(req, res) var middleware = _this.__MIDDLEWARE__.concat() var fn = middleware.shift() + + if (response.rendered) { + return + } + + response.set('X-Powered-By', 'Five.js') + if (fn) { ;(async function next() { await fn.call(_this, request, response, function() { diff --git a/middleware/router.js b/middleware/router.js index 343d7b8..7d529e2 100644 --- a/middleware/router.js +++ b/middleware/router.js @@ -24,8 +24,9 @@ export default function(req, res, next) { // 3. 实例化控制器 this.__MODULES__[req.app] .then(({ default: Mod }) => { - var app, - err = '' + var app + var err = '' + if (Mod) { app = new Mod({ ctx: this, req, res })