Tuesday 26 December 2023

Not Found for an API route

In my Next.js project I have main/app/api/worker-callback/route.ts file:

import { NextApiResponse } from "next";
import { NextResponse } from "next/server";

type ResponseData = {
    error?: string
};

export async function POST(req: Request, res: NextApiResponse<ResponseData>) {
    if (req.headers.get('Authorization') !== process.env.BACKEND_SECRET!) {
        res.status(403).json({ error: "Allowed only by backend" });
        // return Response.json({ error: "Allowed only by backend" }, { status: 403 });
    }
    return NextResponse.json({});
}

But when I query it, I get error 404. Why?

curl -d '' -v -o/dev/null -H "accept: application/json" http://localhost:3000/api/worker-callback
...
< HTTP/1.1 404 Not Found
...

Note that HTML pages in main/app work just fine. I build it by the cd main && next build command.



from Not Found for an API route

No comments:

Post a Comment