LoginSignup
0
0

More than 1 year has passed since last update.

Node.js Express ミドルウェア

Posted at

【Node.js Express ミドルウェア】

Expressで足りないものは、ミドルウェアでカバー

  • リクエストレスポンスに対して、任意の追加処理を行う関数のこと。
    • リクエストオブジェクトの変更
    • レスポンスオブジェクトの変更
    • リクエスト・レスポンスを用いた独自の追加処理 ...etc

ミドルウェアの実装

  • 通常処理
function (req, res, next) {
  //何かの処理 (次の処理を呼べるように next ) 
  next();
}
  •  エラー処理
function (err, req, res, next) {
  //何かの処理 (次の処理を呼べるように next ) ​
 ​next();
}

ミドルウェアの組み込み

const express = require("express");
const app = express();

app.use((req, res, next) => {
    //アプリケーションレベルのミドルウェア
});

app.listen(3000);

※ コードの上から順に、全てのリクエストに対して処理が実行される。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0