ルーティング
Routing refers to how an application’s endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing.
ルーティングはHTTPメソッドに対応するExpressのapp
オブジェクトのメソッドを使用して定義します。たとえば、GETリクエストを処理するapp.get()
やPOSTリクエストを処理するapp.post
があります。
完全なリストについては、app.METHODを参照してください。
また、すべてのHTTPメソッドを制御するためにapp.all()を、ミドルウェアを指定するためにapp.use()をコールバック関数として使用することができます(詳細については、Using middlewareを参照してください)。 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).
これらのルーティングメソッドは、アプリケーションが指定されたルート(エンドポイント)とHTTPメソッドへのリクエストを受け取ったときに呼び出されるコールバック関数(ハンドラ関数とも呼ばれます)を指定します。 つまり、アプリケーションは指定されたルートとメソッドに一致するリクエストをリッスンし、一致を検出すると指定されたコールバック関数を呼び出します。 In other words, the application “listens” for requests that match the specified route(s) and method(s), and when it detects a match, it calls the specified callback function.
In fact, the routing methods can have more than one callback function as arguments.
実際、ルーティングメソッドは複数のコールバック関数を引数として持つことができます。
複数のコールバック関数では、コールバック関数に引数としてnext
を指定し、次のコールバックに制御を渡す関数の本体内でnext()
を呼び出すことが重要です。
次のコードは、極めて基本的なルートの例です。
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')
})
route メソッド
route メソッドは、いずれかの HTTP メソッドから派生され、express
クラスのインスタンスに付加されます。
次のコードは、アプリケーションのルートへの GET メソッドと POST メソッドに定義されたルートの例です。
// GET method route
app.get('/', (req, res) => {
res.send('GET request to the homepage')
})
// POST method route
app.post('/', (req, res) => {
res.send('POST request to the homepage')
})
Expressは、すべてのHTTPリクエストメソッドに対応するメソッド(get
、post
など)をサポートしています。
完全なリストについては、app.METHODを参照して下さい。
For a full list, see app.METHOD.
すべての HTTPリクエストメソッドのパスにミドルウェア関数をロードするために使用される特別なルーティングメソッド、app.all()
があります。 たとえば、GET、POST、PUT、DELETE、またはhttpモジュールでサポートされているその他のHTTPリクエストメソッドを使用するかどうかにかかわらず、”/secret”ルートへのリクエストに対して次のハンドラが実行されます。 For example, the following handler is executed for requests to the route "/secret"
whether using GET
, POST
, PUT
, DELETE
, or any other HTTP request method supported in the http module.
app.all('/secret', (req, res, next) => {
console.log('Accessing the secret section ...')
next() // pass control to the next handler
})
ルート・パス
ルート・パスは、リクエストメソッドとの組み合わせにより、リクエストを実行できるエンドポイントを定義します。ルート・パスは、ストリング、ストリング・パターン、または正規表現にすることができます。 Route paths can be strings, string patterns, or regular expressions.
Caution
In express 5, the characters ?
, +
, *
, []
, and ()
are handled differently than in version 4, please review the migration guide for more information.
Caution
In express 4, regular expression characters such as $
need to be escaped with a \
.
Note
Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Playground Router is a handy tool for testing basic Express routes, although it does not support pattern matching.
Warning
Query strings are not part of the route path.
次に、ストリングに基づくルート・パスの例を示します。
このルート・パスは、リクエストをルートのルート /
にマッチングします。
app.get('/', (req, res) => {
res.send('root')
})
このルート・パスは、リクエストを /about
にマッチングします。
app.get('/about', (req, res) => {
res.send('about')
})
このルート・パスは、リクエストを /random.text
にマッチングします。
app.get('/random.text', (req, res) => {
res.send('random.text')
})
次に、ストリング・パターンに基づくルート・パスの例を示します。
Caution
The string patterns in Express 5 no longer work. Please refer to the migration guide for more information.
このルート・パスは、acd
および abcd
をマッチングします。
app.get('/ab?cd', (req, res) => {
res.send('ab?cd')
})
このルート・パスは、abcd
、abbcd
、abbbcd
などをマッチングします。
app.get('/ab+cd', (req, res) => {
res.send('ab+cd')
})
このルート・パスは、abcd
、abxcd
、abRABDOMcd
、ab123cd
などをマッチングします。
app.get('/ab*cd', (req, res) => {
res.send('ab*cd')
})
このルート・パスは、/abe
および /abcde
をマッチングします。
app.get('/ab(cd)?e', (req, res) => {
res.send('ab(cd)?e')
})
次に、正規表現に基づくルート・パスの例を示します。
このルート・パスは、ルート名に「a」が含まれるすべてのものをマッチングします。
app.get(/a/, (req, res) => {
res.send('/a/')
})
このルート・パスは、butterfly
および dragonfly
をマッチングしますが、butterflyman
、dragonfly man
などはマッチングしません。
app.get(/.*fly$/, (req, res) => {
res.send('/.*fly$/')
})
ルート・パラメータ
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. ルート・パラメータは、URL内の指定された値を取得するために使用されるURLセグメントのことを言います。捕捉された値はreq.params
オブジェクトの中で、パスに指定されたルート・パラメータの名前をそれぞれのキーとして設定されます。
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
ルート・パラメータを使用してルートを定義するには、以下に示すようにルートのパスにルート・パラメータを指定するだけです。
app.get('/users/:userId/books/:bookId', (req, res) => {
res.send(req.params)
})
ルート・パラメータの名前は、「単語文字」([A-Za-z0-9_])で構成する必要があります。
ハイフン(-
)とドット(.
)は文字通りに解釈されるので、有用な目的のためにルート・パラメータとともに使用することができます。
Route path: /flights/:from-:to
Request URL: http://localhost:3000/flights/LAX-SFO
req.params: { "from": "LAX", "to": "SFO" }
Route path: /plantae/:genus.:species
Request URL: http://localhost:3000/plantae/Prunus.persica
req.params: { "genus": "Prunus", "species": "persica" }
Caution
In express 5, Regexp characters are not supported in route paths, for more information please refer to the migration guide.
ルート・パラメータで一致させることができる正確な文字列をより詳細に制御するために、括弧(()
)内で正規表現を追加できます:
Route path: /user/:userId(\d+)
Request URL: http://localhost:3000/user/42
req.params: {"userId": "42"}
Warning
Because the regular expression is usually part of a literal string, be sure to escape any \
characters with an additional backslash, for example \\d+
.
Warning
In Express 4.x, the *
character in regular expressions is not interpreted in the usual way. As a workaround, use {0,}
instead of *
. This will likely be fixed in Express 5.
ルート・ハンドラー
リクエストを処理するために、ミドルウェアのように動作する複数のコールバック関数を指定できます。唯一の例外は、これらのコールバックが next('route')
を呼び出して、残りのルート・コールバックをバイパスすることです。このメカニズムを使用して、ルートに事前条件を適用し、現在のルートで続行する理由がない場合に後続のルートに制御を渡すことができます。 The only exception is that these callbacks might invoke next('route')
to bypass the remaining route callbacks. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there’s no reason to proceed with the current route.
次の例に示すように、ルート・ハンドラーの形式は、関数、関数の配列、または両方の組み合わせにすることができます。
単一のコールバック関数で 1 つのルートを処理できます。次に例を示します。 For example:
app.get('/example/a', (req, res) => {
res.send('Hello from A!')
})
複数のコールバック関数で1つのルートを処理できます (必ず、next
オブジェクトを指定してください)。次に例を示します。 For example:
app.get('/example/b', (req, res, next) => {
console.log('the response will be sent by the next function ...')
next()
}, (req, res) => {
res.send('Hello from B!')
})
コールバック関数の配列で 1 つのルートを処理できます。次に例を示します。 For example:
const cb0 = function (req, res, next) {
console.log('CB0')
next()
}
const cb1 = function (req, res, next) {
console.log('CB1')
next()
}
const cb2 = function (req, res) {
res.send('Hello from C!')
}
app.get('/example/c', [cb0, cb1, cb2])
独立した関数と、関数の配列の組み合わせで1つのルートを処理できます。次に例を示します。 For example:
const cb0 = function (req, res, next) {
console.log('CB0')
next()
}
const cb1 = function (req, res, next) {
console.log('CB1')
next()
}
app.get('/example/d', [cb0, cb1], (req, res, next) => {
console.log('the response will be sent by the next function ...')
next()
}, (req, res) => {
res.send('Hello from D!')
})
レスポンスメソッド
次の表に示すレスポンスオブジェクト (res
) のメソッドは、レスポンスをクライアントに送信して、リクエストとレスポンスのサイクルを終了することができます。これらのメソッドのいずれもルート・ハンドラーから呼び出されない場合、クライアントリクエストはハングしたままになります。 If none of these methods are called from a route handler, the client request will be left hanging.
メソッド | 説明 |
---|---|
res.download() | ファイルのダウンロードのプロンプトを出します。 |
res.end() | レスポンスプロセスを終了します。 |
res.json() | JSON レスポンスを送信します。 |
res.jsonp() | JSONP をサポートする JSON レスポンスを送信します。 |
res.redirect() | リクエストをリダイレクトします。 |
res.render() | ビュー・テンプレートをレンダリングします。 |
res.send() | さまざまなタイプのレスポンスを送信します。 |
res.sendFile | ファイルをオクテット・ストリームとして送信します。 |
res.sendStatus() | レスポンスのステータスコードを設定して、そのストリング表現をレスポンス本文として送信します。 |
app.route()
You can create chainable route handlers for a route path by using app.route()
.
Because the path is specified at a single location, creating modular routes is helpful, as is reducing redundancy and typos. For more information about routes, see: Router() documentation.
次に、app.route()
を使用して定義された、チェーニングされたルート・ハンドラーの例を示します。
app.route('/book')
.get((req, res) => {
res.send('Get a random book')
})
.post((req, res) => {
res.send('Add a book')
})
.put((req, res) => {
res.send('Update the book')
})
express.Router
モジュール式のマウント可能なルート・ハンドラーを作成するには、express.Router
クラスを使用します。Router
インスタンスは、完全なミドルウェアおよびルーティング・システムです。そのため、よく「ミニアプリケーション」と呼ばれます。 A Router
instance is a complete middleware and routing system; for this reason, it is often referred to as a “mini-app”.
次の例では、ルーターをモジュールとして作成し、その中にミドルウェア関数をロードして、いくつかのルートを定義し、ルート・モジュールをメインアプリケーションのパスにマウントします。
アプリケーション・ディレクトリーに次の内容で birds.js
というルーター・ファイルを作成します。
const express = require('express')
const router = express.Router()
// middleware that is specific to this router
const timeLog = (req, res, next) => {
console.log('Time: ', Date.now())
next()
}
router.use(timeLog)
// define the home page route
router.get('/', (req, res) => {
res.send('Birds home page')
})
// define the about route
router.get('/about', (req, res) => {
res.send('About birds')
})
module.exports = router
次に、ルーター・モジュールをアプリケーションにロードします。
const birds = require('./birds')
// ...
app.use('/birds', birds)
これで、アプリケーションは、/birds
および /birds/about
に対するリクエストを処理するほか、ルートに固有の timeLog
ミドルウェア関数を呼び出すことができるようになります。
But if the parent route /birds
has path parameters, it will not be accessible by default from the sub-routes. To make it accessible, you will need to pass the mergeParams
option to the Router constructor reference.
const router = express.Router({ mergeParams: true })