API Responses with app dir
Unanswered
Brown bear posted this in #help-forum
Brown bearOP
Hi yall,
Regarding building out API routes with app dir, would it generally be more appropriate to use node's built in
Regarding building out API routes with app dir, would it generally be more appropriate to use node's built in
request and response? utilizing NextResponse and NextRequest seem to result in 500 error codes occuring.import { NextResponse } from "next/server";
export const runtime = "edge";
export async function POST(req: Request) {
const { url } = (await req.json()) as { url?: string };
if (!url) {
return new Response("Missing url", { status: 400 });
}
try {
const foobar = {foo: "bar"}
return NextResponse.json(foobar);
} catch (error) {
const message = error instanceof Error ? error.message : (error as string);
return new Response("Error!", { status: 500 });
}
}2 Replies
Northern Wheatear
I'm confused with the code.. Why are are using both new Response and NextResponse at the same time.
Just use NextResponse.json everywhere
Just use NextResponse.json everywhere
@Northern Wheatear I'm confused with the code.. Why are are using both new Response and NextResponse at the same time.
Just use NextResponse.json everywhere
Brown bearOP
The instance of regular response causes a 500 error for some reason, and same when applying status codes 🤔