简体   繁体   English

如何在 nextjs 服务器中添加中间件

[英]how to add middleware in nextjs server

I am trying to add a middleware in nextjs server like the below node express server uploadFile.single("")我正在尝试在 nextjs 服务器中添加一个中间件,例如下面的节点快递服务器uploadFile.single("")

app.post("/upload-excel", uploadFile.single('file'), async (req, res) => {

Here is the nextjs server code, where I need to add the uploadFile.single('file')这是nextjs服务器代码,我需要在其中添加uploadFile.single('file')

export default function handler(req: any, res: NextApiResponse<Data>) {
  });
}

Create a _middleware file and handle based on your specific routes :创建一个 _middleware 文件并根据您的特定路线进行处理:

export function middleware(request: NextRequest) {
  const myCase =
    request.nextUrl.pathname.startsWith('/upload-excel');
     

  if (myCase) {
    //handle here
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM