Middleware

Node.js Middleware

使用 Middleware

var express = require('express');
const app = express();

app.use(express.urlencoded({ extended: false}));

app.use(express.json());

app.use(express.static(path.join(__dirname, '/public')));

內建 Middleware

變數 說明
express.urlencoded() 網址編碼處理,e.g. content-type = application/x-www-form-urlencoded
express.json() JSON 資料解析
express.static() 靜態檔案處理

客製化 Middleware

var express = require('express');
const app = express();

app.use((request, response, next) => {
  console.log(`${request.method} ${request.path}`);
  next();
});