9.6 KiB
layout, title, description, menu, order, redirect_from
| layout | title | description | menu | order | redirect_from |
|---|---|---|---|---|---|
| page | Express アプリケーションで使用するミドルウェアの作成 | Learn how to write custom middleware functions for Express.js applications, including examples and best practices for enhancing request and response handling. | guide | 2 |
Express アプリケーションで使用するミドルウェアの作成
概説
Middleware functions are functions that have access to the [request object](/{{ page.lang }}/5x/api.html#req) (req), the [response object](/{{ page.lang }}/5x/api.html#res) (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 メソッド。 |
Starting with Express 5, middleware functions that return a Promise will call next(value) when they reject or throw an error. next will be called with either the rejected value or the thrown Error.
例
Here is an example of a simple "Hello World" Express application.
The remainder of this article will define and add three middleware functions to the application:
one called myLogger that prints a simple log message, one called requestTime that
displays the timestamp of the HTTP request, and one called validateCookies that validates incoming cookies.
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(3000)
ミドルウェア関数 myLogger
Here is a simple example of a middleware function called "myLogger". This function just prints "LOGGED" when a request to the app passes through it. 次に、"myLogger"というミドルウェア関数の簡単な例を示します。この関数は、アプリケーションへのリクエストがそれを通過するときに、単に "LOGGED"を出力します。ミドルウェア関数は、`myLogger`という名前の変数に割り当てられます。const myLogger = function (req, res, next) {
console.log('LOGGED')
next()
}
To load the middleware function, call app.use(), specifying the middleware function.
ミドルウェア関数をロードするには、ミドルウェア関数を指定して app.use() を呼び出します。
例えば、次のコードは、ルート・パス (/) へのルートの前に myLogger ミドルウェア関数をロードします。
const express = require('express')
const app = express()
const myLogger = function (req, res, next) {
console.log('LOGGED')
next()
}
app.use(myLogger)
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(3000)
アプリケーションは、リクエストを受け取るたびに、端末にメッセージ「LOGGED」を出力します。
ミドルウェアのロードの順序は重要です。最初にロードされたミドルウェア関数が常に最初に実行されます。
myLogger がルート・パスの後にロードされた場合、リクエストが到達することはなく、アプリケーションは「LOGGED」を出力しません。ルート・パスのルート・ハンドラーがリクエストレスポンスサイクルを終了するためです。
ミドルウェア関数 myLogger は、単にメッセージを出力してから、next() 関数を呼び出して、スタック内の次のミドルウェア関数へのリクエストに移ります。
ミドルウェア関数 requestTime
次に、「requestTime」というミドルウェア関数を作成し、requestTimeというプロパティとしてリクエストオブジェクトに追加します。
const requestTime = function (req, res, next) {
req.requestTime = Date.now()
next()
}
The app now uses the requestTime middleware function. これで、アプリケーションが requestTime ミドルウェア関数を使用するようになります。また、ルート・パス・ルートのコールバック関数は、ミドルウェア関数が req (リクエストオブジェクト) に追加するプロパティーを使用します。
const express = require('express')
const app = express()
const requestTime = function (req, res, next) {
req.requestTime = Date.now()
next()
}
app.use(requestTime)
app.get('/', (req, res) => {
let responseText = 'Hello World!<br>'
responseText += `<small>Requested at: ${req.requestTime}</small>`
res.send(responseText)
})
app.listen(3000)
アプリケーションのルートにリクエストすると、アプリケーションは、リクエストのタイムスタンプをブラウザーに表示します。
Middleware function validateCookies
Finally, we'll create a middleware function that validates incoming cookies and sends a 400 response if cookies are invalid.
Here's an example function that validates cookies with an external async service.
async function cookieValidator (cookies) {
try {
await externallyValidateCookie(cookies.testCookie)
} catch {
throw new Error('Invalid cookies')
}
}
Here, we use the cookie-parser middleware to parse incoming cookies off the req object and pass them to our cookieValidator function. The validateCookies middleware returns a Promise that upon rejection will automatically trigger our error handler.
const express = require('express')
const cookieParser = require('cookie-parser')
const cookieValidator = require('./cookieValidator')
const app = express()
async function validateCookies (req, res, next) {
await cookieValidator(req.cookies)
next()
}
app.use(cookieParser())
app.use(validateCookies)
// error handler
app.use((err, req, res, next) => {
res.status(400).send(err.message)
})
app.listen(3000)
リクエストオブジェクト、レスポンスオブジェクト、スタック内の次のミドルウェア関数、および Node.js API を利用できるため、ミドルウェア関数が持つ可能性は無限です。
Express ミドルウェアについて詳しくは、[Express ミドルウェアの使用](/{{ page.lang }}/guide/using-middleware.html)を参照してください。
設定可能なミドルウェア
ミドルウェアを設定可能にする必要がある場合は、optionsオブジェクトまたはその他のパラメータを受け入れる関数をエクスポートし、入力パラメータに基づいてミドルウェアの実装を返します。
File: my-middleware.js
module.exports = function (options) {
return function (req, res, next) {
// Implement the middleware function based on the options object
next()
}
}
ミドルウェアは以下のように使用できるようになりました。
const mw = require('./my-middleware.js')
app.use(mw({ option1: '1', option2: '2' }))
設定可能なミドルウェアの例については、cookie-sessionおよびcompressionを参照してください。
