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();
});