I created a new Next.js@13.0.5
project with Turbopack
and added middleware.ts
file to the project's root directory. When I sent a request(e.g. /api/search?query=Hello
), the middleware wasn't executed. Where is the location of middleware.ts
file in the latest Next.js
?
My middleware looks like:
import { NextRequest } from 'next/server';
import { query } from 'express-validator';
import validateRequest from '@/lib/request-validation';
export function middleware(request: NextRequest) {
console.log('middleware', request.nextUrl.pathname);
if (
request.method === 'GET' &&
request.nextUrl.pathname.startsWith('/search')
) {
const res = validateRequest([
query('query').isString(),
query('page').isNumeric(),
query('perPage').isNumeric(),
]);
return res;
}
}