I wrote NextJS middleware that rewrites request to other internal server
import {NextRequest, NextResponse, URLPattern} from "next/server";
import {endpoint} from "@/utils";
export const config = {
matcher: "/api/:path*"
}
const pattern = new URLPattern({pathname: "/api/:path*"})
export async function middleware(req: NextRequest) {
const result = pattern.exec(req.url)
console.log(result)
if (result) {
const path = result.pathname.groups.path
return NextResponse.rewrite(`${endpoint}/${path}`)
}
}
this code works for GET method but does not for POST method(internal server doesn’t receive request body)
when I wrote rewrite in next.config.js
it worked
async rewrites() {
return [
{
source: "/api/:path*",
destination: `${process.env.API_ENDPOINT}/:path*`,
},
]
},
How can I get it work using NextJS middleware?
I need to use NextJS middleware to append additional headers to request and that next.config.js
method does not work either when I create middleware function(even if that function does nothing at all)