| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,6 +111,22 @@ Rackを参考にして実装されています。 | |||
| 111 | 111 | ||
| 112 | 112 | - [Ruby - Rack解説 - Rackの構造とRack DSL - Qiita](http://qiita.com/higuma/items/838f4f58bc4a0645950a#2-5 "Ruby - Rack解説 - Rackの構造とRack DSL - Qiita") | |
| 113 | 113 | ||
| 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) | ||
| 115 | 132 | ||
| 116 | - ## 実装してみよう | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | "use strict"; | |
| 2 | 2 | import Junction from "./junction"; | |
| 3 | - import assert from "power-assert"; | ||
| 3 | + import assert from "assert"; | ||
| 4 | 4 | let junction = new Junction(); | |
| 5 | 5 | junction.use(function toUpperCase(res, next) { | |
| 6 | 6 | res.value = res.value.toUpperCase(); | |
@@ -10,7 +10,7 @@ junction.use(function exclamationMark(res, next) { | |||
| 10 | 10 | res.value = res.value + "!"; | |
| 11 | 11 | next(); | |
| 12 | 12 | }); | |
| 13 | - junction.use(function (error, res, next) { | ||
| 13 | + junction.use(function errorHandling(error, res, next) { | ||
| 14 | 14 | console.error(error.stack); | |
| 15 | 15 | next(); | |
| 16 | 16 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments