使用egg.js框架開發項目,使用中間件做攔截器app/middleware/auth.js中做簡單的判斷session中user是否為空對象,登陸頁面不做攔截module.exports = options => { return function* auth(next) { yield next; if(this.path=='/'||this.path=='/login'){ return; } if(!this.session.user.username){ this.redirect('/', 'login.index'); } };};然后再app/config/config.default.js中添加中間件的配置config.middleware=[ 'auth' ];在登陸的api中有一系列的判斷,然后會返回相應的code 和message, this.ctx.body = { 'code':code, 'message':message };但是添加了中間件后前臺點擊登陸,登錄api中也能打印出相應的code和message,但是前臺發送登錄請求的回調函數接收到的相應參數的body成了當前登陸頁面
EggJs使用中間件做攔截器出現問題
滄海一幻覺
2018-12-21 19:25:35