File tree Expand file tree Collapse file tree 2 files changed +20
-4
lines changed Expand file tree Collapse file tree 2 files changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,22 @@ Rackを参考にして実装されています。
111111
112112- [ Ruby - Rack解説 - Rackの構造とRack DSL - Qiita] ( http://qiita.com/higuma/items/838f4f58bc4a0645950a#2-5 " Ruby - Rack解説 - Rackの構造とRack DSL - Qiita ")
113113
114- 次は、先ほど抽象的なコードとなっていたものを、具体的な実装にしていきます。
114+ 次は、先ほど抽象的なコードとなっていたものを具体的な実装にしながら見ていきます。
115+
116+ ## 実装してみよう
117+
118+ ` Junction ` というConnectライクな_middleware_をサポートしたものを作成してみます。
119+
120+ ` Junction ` は、` use(middleware) ` と ` process(value, (error, result) => { }); ` を持っているシンプルなクラスです。
121+
122+ [ import junction.js] ( ../../src/connect/junction.js )
123+
124+ 実装を見てみると、` use ` で_middleware_を登録して、` process ` で登録したミドルウェアを順番に実行していきます。
125+ そのため、` Junction ` 自体は渡されたデータは何も処理せずに、_ middleware_との中継のみをしています。
126+
127+ 登録する_middleware_はConnectと同じで、処理をしたら` next ` を呼んで、次の_middleware_が処理するというのを繰り返しています。
128+
129+ 使い方はConnectと引数の違いはありますが、ほぼ同じような形で利用できます。
130+
131+ [ import junction-example.js] ( ../../src/connect/junction-example.js )
115132
116- ## 実装してみよう
Original file line number Diff line number Diff line change 11"use strict" ;
22import Junction from "./junction" ;
3- import assert from "power- assert" ;
3+ import assert from "assert" ;
44let junction = new Junction ( ) ;
55junction . use ( function toUpperCase ( res , next ) {
66 res . value = res . value . toUpperCase ( ) ;
@@ -10,7 +10,7 @@ junction.use(function exclamationMark(res, next) {
1010 res . value = res . value + "!" ;
1111 next ( ) ;
1212} ) ;
13- junction . use ( function ( error , res , next ) {
13+ junction . use ( function errorHandling ( error , res , next ) {
1414 console . error ( error . stack ) ;
1515 next ( ) ;
1616} ) ;
You can’t perform that action at this time.
0 commit comments