While I'm sure there are many ways to do it, the way that I'm following is the way that is documented in the NextJS docs here.
In Typescript
for clarity:
import { NextResponse } from 'next/server'
export async function GET(request: Request) {
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
}
and Javascript
for those that prefer it:
import { NextResponse } from 'next/server'
export async function GET(request) {
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
}
Note: I have experimented with replacing the word error
in { error: 'Internal Server Error' } with {statusText: 'Internal Server Error' } since this is the default spec in the Web API; also, when you console log the received object from your fetch in NextJS (ie. when you console log that which is returned from your API) that key error
turns to statusText
. Either way has worked fine.