ルーティング

Routing とは、アプリケーションのエンドポイント(URI)がクライアントリクエストに対してどのように応答するかを指します。 For an introduction to routing, see Basic routing. For an introduction to routing, see Basic routing.

HTTPメソッドに対応するExpress app オブジェクトのメソッドを使用してルーティングを定義します。 のように、app。 POST リクエストを処理する GET リクエストと app.post` を処理します。 For a full list, see app.METHOD. You can also use app.all() to handle all HTTP methods and app.use() to specify middleware as the callback function (See Using middleware for details).

These routing methods specify a callback function (sometimes called a “handler function”) that Express automatically runs when the application receives a request matching the specified route (endpoint) and HTTP method. 言い換えれば、アプリケーションは指定されたルートとメソッドに一致するリクエストを「リッスン」します。 マッチを検出すると、指定されたコールバック関数を呼び出します。

実際、ルーティングメソッドは引数として複数のコールバック関数を持つことができます。 複数のコールバック関数を使用。 コールバック関数に next を引数として渡し、関数の本体内で next() を呼び出して、次のコールバックに を渡すことが重要です。 複数のコールバック関数を使用。 コールバック関数に next を引数として渡し、関数の本体内で next() を呼び出して、次のコールバックに を渡すことが重要です。

以下のコードは、非常に基本的なルートの例です。

index.cjs
const express = require('express');
const app = express();
// respond with "hello world" when a GET request is made to the homepage
app.get('/', (req, res) => {
res.send('hello world');
});