Skip to content

feat(connect): メリットとデメリットついて #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 19, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions ja/connect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ Rackを参考にして実装されています。

## 実装してみよう

`Junction`というConnectライクな_middleware_をサポートしたものを作成してみます
JunctionというConnectライクな_middleware_をサポートしたものを作成してみます

`Junction`は、`use(middleware)` と `process(value, (error, result) => { });`を持っているシンプルなクラスです。
Junctionは、`use(middleware)` と `process(value, (error, result) => { });`を持っているシンプルなクラスです。

[import junction.js](../../src/connect/junction.js)

Expand All @@ -130,3 +130,35 @@ Rackを参考にして実装されています。

[import junction-example.js](../../src/connect/junction-example.js)


## どういう用途に向いている?

ConnectやJunctionの実装を見てみると分かりますが、このアーキテクチャでは機能の詳細は_middleware_で実装できます。
そのため、本体の実装は_middleware_に提供するインタフェースの決定、エラーハンドリングの手段の提供するだけでとても小さいものとなっています。

今回は紹介していませんが、Connectにはルーティングに関する機能があります。
しかし、この機能も「与えられたパスにマッチした場合のみに反応する_middleware_を登録する」という単純なものです。

```js
app.use("/foo", function fooMiddleware(req, res, next) {
// req.url starts with "/foo"
next();
});
```

このアーキテクチャは、入力があり出力がある場合にコアとなる部分は小さく実装できることが分かります。

そのため、ConnectやRackなどのHTTPサーバでは「リクエストに対してレスポンスを返す」というのが決まっているので、
このアーキテクチャは適しています。

## どういう用途に向いていない?

このアーキテクチャでは機能の詳細が_middleware_で実装できます。
その中で多くの機能を_middleware_で実装していくと、_middleware_間に依存関係が生じることがあります。

これにより、`use(middleware)` で登録する順番が変わるだけで挙動が変わる事があります。
_middleware_は柔軟ですが、_middleware_間で起きる前提の解決を利用者が行う必要があります。

そのため、プラグイン同士の独立性や明確な依存関係を扱いたい場合には不向きといえるでしょう。

これらを解消するためにコアはそのままにして、最初から幾つかの_middleware stack_を作ったものが提供されるケースもあります。