logo

该视频仅会员有权观看

立即开通课程「Next.js 前端应用开发实践」权限。

¥
199
/ 年

在匹配的路径上运行中间件

  • Max is middleware for ES that executes on each request.
  • It can be configured to run only at specific addresses using two methods.
  • The first method involves exporting a config object with 'matter' to match address patterns, limiting middleware to run only at those addresses.
  • Config objects can contain 'matter' as strings or arrays for matching patterns, with many syntax options detailed in the middleware documentation.
  • Illustrative examples show how to monitor terminal outputs for requests to the homepage, an about page, and a video content list page, noting that middleware execution is confined to certain paths.
  • To have middleware run for single content page access, patterns like 'post/:id/' are added to the 'matter' configuration.
  • Alternatively, middleware can internally check request path names to control its execution, bypassing the need for config export.
  • By analyzing the request, specifically 'next url', and utilizing string methods like 'startsWith', middleware can be made to run only for content list and individual content page accesses.