Express アプリケーションで使用するミドルウェアの作成
概説
Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle. The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.
ミドルウェア関数は以下のタスクを実行できます。
- 任意のコードを実行する。
- リクエストオブジェクトとレスポンスオブジェクトを変更する。
- リクエストレスポンスサイクルを終了する。
- スタック内の次のミドルウェアを呼び出す。
現在のミドルウェア関数がリクエストレスポンスサイクルを終了しない場合は、next() を呼び出して、次のミドルウェア関数に制御を渡す必要があります。そうしないと、リクエストはハングしたままになります。 Otherwise, the request will be left hanging.
次の例は、ミドルウェア関数呼び出しの要素を示しています。
|
ミドルウェア関数が適用される HTTP メソッド。 </tbody>
ミドルウェア関数が適用されるパス (ルート)。
ミドルウェア関数。
ミドルウェア関数へのコールバック引数 (慣習的に「next」と呼ばれます)。
HTTP response argument to the middleware function, called "res" by convention.
HTTP request argument to the middleware function, called "req" by convention.
|