如何在特定路径而不是其子路径上定位中间件


app.use('/foo/', myMiddleware, index);
app.use('/foo/image.svg', image);
app.use('/foo/other-page/', otherPage);

我只在/foo/路线上设置了myMiddleware

但是 Express 在上述所有路由上运行myMiddleware(可能是因为其他路由是第一个路由的子路径(。

如何配置它,以便myMiddleware只能在/foo/路由上工作?

找到了答案,回答了我自己的问题。

如果使用.get()而不是.use()这将确保中间件仅在指定的路由上工作。

最新更新