Skip to content

Connectの仕組み #57

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

Closed
4 tasks done
azu opened this issue Sep 13, 2015 · 0 comments · Fixed by #60
Closed
4 tasks done

Connectの仕組み #57

azu opened this issue Sep 13, 2015 · 0 comments · Fixed by #60

Comments

@azu
Copy link
Owner

azu commented Sep 13, 2015

#22 の一部

Connectの仕組みについてまとめる。

思想的にはRackから来てて、RackのものはPythonのWSGIミドルウェアから影響受けてそう。

pylons_as_onion

おおまかな仕組みとしては

  • .use(middlware) はmiddlewareをstackに追加する
  • リクエストがきたら、stack.forEach(middleware => middleware(req, res, next)) する

というだけ。
エラーハンドリングはmiddlewareの引数の数で分けている。

後はルーティングの仕組みもあるけど、middlewareとは別に関係ないのでこの書籍は省略する。

内容

  • middlewareをトレースしてみる
  • useがスタックに詰めて、call nextをしている仕組み
  • エラーハンドリング middleware
  • Rackから影響を受けている話

この仕組みはHTTPサーバのミドルウェアスタックの話と深い関係がありそうなので、
あんまり深追いはしないでHTTPサーバではよくある仕組みだよみたいな話に留める。


koaの場合はgeneratorを使うので少しnextの扱い方が異なっている。
Connectの場合はnextは終了の的な意味合いだけど、 yeild nextと書けば、そのmiddlewareは前後の処理も書くことができる。

エラーハンドリングはそれを上手く使ってる。

  try {
    yield next;
  } catch (err) {
    this.status = err.status || 500;
    this.body = err.message;
    this.app.emit('error', err, this);
  }
@azu azu mentioned this issue Sep 13, 2015
7 tasks
azu added a commit that referenced this issue Sep 15, 2015
@azu azu mentioned this issue Sep 15, 2015
@azu azu closed this as completed in #60 Sep 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant