Next.js Discord

Discord Forum

trying to use NextApiRequest, NextApiResponse

Answered
Bombay-duck posted this in #help-forum
Open in Discord
Bombay-duckOP
i had a server with express and now am trying to translate it to next server, but i don't quite get how to use NextApiRequest, NextApiResponse, i've read that the usual nextrequest and nextresponse run on the Edge Middleware or Functions & it's not recommended to use it since i'll be communicating password and user data through that api call with the Database,

when i tried to use NextApi for example :
import type { NextApiRequest, NextApiResponse } from 'next'
 
type Data = {
 name: string
}
 
export default function handler(
 req: NextApiRequest,
 res: NextApiResponse<Data>
) {
 res.status(200).json({ name: 'John Doe' })
}

a real simple route but it just doesn't work, am using an app router in my project, i get this error :
 ⨯ Detected default export in 'C:\Users\papoo\OneDrive\Documents\1code\MindForgeNext\Frontend\app\api\sendvoice\route.ts'. Export a named export for each HTTP method instead.
 ⨯ No HTTP methods exported in 'C:\Users\papoo\OneDrive\Documents\1code\MindForgeNext\Frontend\app\api\sendvoice\route.ts'. Export a named export for each HTTP method.


if remove handle and default like this :
import type { NextApiRequest, NextApiResponse } from 'next'
 
type Data = {
 name: string
}
 
export function POST(
 req: NextApiRequest,
 res: NextApiResponse<Data>
) {
 res.status(200).json({ name: 'John Doe' })
}

another error is thrown :
⨯ TypeError: res.status is not a function
    at POST (webpack-internal:///(rsc)/./app/api/sendvoice/route.ts:6:9)

any idea how to solve that and if i move to page route will solve this problem ?
Please help and thank you in advane !
Answered by Ray
NextApiRequest & NextApiResponse only work with api route in page router.
seem like you are using app router so you should create the route like this
// app/api/sendvoice/route.ts
export function POST(
 req: Request
) {
 return Response.json({ name: 'John Doe'}, { status: 200 })
}
View full answer

3 Replies

@Bombay-duck i had a server with express and now am trying to translate it to next server, but i don't quite get how to use NextApiRequest, NextApiResponse, i've read that the usual nextrequest and nextresponse run on the Edge Middleware or Functions & it's not recommended to use it since i'll be communicating password and user data through that api call with the Database, when i tried to use NextApi for example : import type { NextApiRequest, NextApiResponse } from 'next' type Data = { name: string } export default function handler( req: NextApiRequest, res: NextApiResponse<Data> ) { res.status(200).json({ name: 'John Doe' }) } a real simple route but it just doesn't work, am using an app router in my project, i get this error : ⨯ Detected default export in 'C:\Users\papoo\OneDrive\Documents\1code\MindForgeNext\Frontend\app\api\sendvoice\route.ts'. Export a named export for each HTTP method instead. ⨯ No HTTP methods exported in 'C:\Users\papoo\OneDrive\Documents\1code\MindForgeNext\Frontend\app\api\sendvoice\route.ts'. Export a named export for each HTTP method. if remove handle and default like this : import type { NextApiRequest, NextApiResponse } from 'next' type Data = { name: string } export function POST( req: NextApiRequest, res: NextApiResponse<Data> ) { res.status(200).json({ name: 'John Doe' }) } another error is thrown : ⨯ TypeError: res.status is not a function at POST (webpack-internal:///(rsc)/./app/api/sendvoice/route.ts:6:9) any idea how to solve that and if i move to page route will solve this problem ? Please help and thank you in advane !
NextApiRequest & NextApiResponse only work with api route in page router.
seem like you are using app router so you should create the route like this
// app/api/sendvoice/route.ts
export function POST(
 req: Request
) {
 return Response.json({ name: 'John Doe'}, { status: 200 })
}
Answer
Bombay-duckOP
clean answer, thank you.
i don't have other question, with this the forum can be closed.