使用中间件模式,让代码和 ExpressJS, ReduxJS 一样容易扩展 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
unbug
V2EX    前端开发

使用中间件模式,让代码和 ExpressJS, ReduxJS 一样容易扩展

  •  
  •   unbug 2016-12-23 14:20:41 +08:00 2034 次点击
    这是一个创建于 3295 天前的主题,其中的信息可能已经有所发展或是发生改变。

    ExpressJS 和 ReduxJS 的易扩展性正是得益于应用了中间件的设计模式。如果想在自己平日的项目里使用,可以试试 Middleware.js 这个中间件工具,它能使任意对象中间件化,用好中间件能有效解藕代码也易于维护。

    MiddlewareJS 已发布到 GitHub

    Readme

    Apply middleweare to an object.

    Middleware functions are functions that have access to the target function and it's arguments, and the target object and the next middleware function in the target function cycle. The next middleware function is commonly denoted by a variable named next.

    Middleware functions can perform the following tasks:

    • Execute any code.
    • Make changes to the function's arguments.
    • End the target function.
    • Call the next middleware in the stack.

    If the current middleware function does not end the target function cycle, it must call next() to pass control to the next middleware function. Otherwise, the target function will be left hanging.

    API

    .use(methodName, ...middlewares)

    Apply middleware to the target object. If the first argument is a middleware object, the rest arguments must be middleware objects.

    • {string|object} methodName String for target function name, object for a middleware object.
    • {...function} middlewares The middleware chain to be applied.
    • return {object} this

    Usages

    Basic

    We define a Person class.

    // the target object class Person { // the target function walk(step) { this.step = step; } speak(word) { this.word = word; } } 

    Then we define a middleware function to print log.

     // middleware for walk function const logger = target => next => (...args) => { console.log(`walk start, steps: ${args[0]}.`); const result = next(...args); console.log(`walk end.`); return result; } 

    Now we apply the log function as a middleware to a Person instance.

     // apply middleware to target object const p = new Person(); const applyMiddleware = new ApplyMiddleware(p); applyMiddleware.use('walk', walk); p.walk(3); 

    Whenever a Person instance call it's walk method, we'll see logs from the looger middleware.

    Middleware object

    We can also apply a middleware object to a target object. Middleware object is an object that contains function's name as same as the target object's function name. Function's name start or end with "_" will not be able to apply middleware.

    const PersonMiddleware { walk: target => next => (step) => { console.log(`walk start, steps: step.`); const result = next(step); console.log(`walk end.`); return result; }, speak: target => next => (word) => { word = 'this is a middleware trying to say: ' + word; return next(word); } } // apply middleware to target object const p = new Person(); const applyMiddleware = new ApplyMiddleware(p); applyMiddleware.use(PersonMiddleware); p.walk(3); p.speak('hi'); 

    middlewareMethods

    Or we can use middlewareMethods to define function names for middleweare target within a class.

    class CuePointMiddleware { constructor() { /** * Define function names for middleweare target. * @type {Array} */ this.middlewareMethods = ['walk', 'speak']; } log(text) { console.log('Middleware log: ' + text); } walk(target) { return next => (step) => { this.log(`walk start, steps: step.`); const result = next(step); this.log(`walk end.`); return result; } } speak(target) { return next => (word) => { this.log('this is a middleware tring to say: ' + word); return next(word); } } } // apply middleware to target object const p = new Person(); const applyMiddleware = new ApplyMiddleware(p); applyMiddleware.use(new PersonMiddleware()) p.walk(3); p.speak('hi'); 
    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3566 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 10:10 PVG 18:10 LAX 02:10 JFK 05:10
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86