always getting 404
Unanswered
White-tailed Tropicbird posted this in #help-forum
White-tailed TropicbirdOP
I'm using the src/app structure, and my current project structure is
/app/api/get-connection-ids.ts (gives 404)
/app/layout.tsx
/app/page.tsx (trying to fetch /api/get-connection-ids from here)
and the definition of get-connection-ids.ts is
/app/api/get-connection-ids.ts (gives 404)
/app/layout.tsx
/app/page.tsx (trying to fetch /api/get-connection-ids from here)
and the definition of get-connection-ids.ts is
"use server";
import type { NextApiRequest, NextApiResponse } from "next";
import ConnectionIds from "@/types/api-responses/ConnectionIds";
export default function handler(req: NextApiRequest, res: NextApiResponse<ConnectionIds>) {
if (req.method !== 'GET') {
res.status(405).end();
return;
}
res.status(200).json({
ids: ["Connection 1", "Connection 2"]
});
}12 Replies
@White-tailed Tropicbird I'm using the src/app structure, and my current project structure is
/app/api/get-connection-ids.ts (gives 404)
/app/layout.tsx
/app/page.tsx (trying to fetch /api/get-connection-ids from here)
and the definition of get-connection-ids.ts is
ts
"use server";
import type { NextApiRequest, NextApiResponse } from "next";
import ConnectionIds from "@/types/api-responses/ConnectionIds";
export default function handler(req: NextApiRequest, res: NextApiResponse<ConnectionIds>) {
if (req.method !== 'GET') {
res.status(405).end();
return;
}
res.status(200).json({
ids: ["Connection 1", "Connection 2"]
});
}
// app/api/get-connection-ids/route.ts
import ConnectionIds from "@/types/api-responses/ConnectionIds";
export function GET(req: Request) {
return Response.json({
ids: ["Connection 1", "Connection 2"]
});I think you should read the doc
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
White-tailed TropicbirdOP
I read this doucumentation: https://nextjs.org/docs/pages/building-your-application/routing/api-routes
@Ray I think you should read the doc
https://nextjs.org/docs/app/building-your-application/routing/route-handlers
read this if you are using app router
White-tailed TropicbirdOP
I see, thanks
no prob
White-tailed TropicbirdOP
the file msut be named route.ts? or i can make different files such as get.ts, post.ts blah blah?
then inside
export async function GET(request: Request) {}
export async function HEAD(request: Request) {}
export async function POST(request: Request) {}
export async function PUT(request: Request) {}
export async function DELETE(request: Request) {}
export async function PATCH(request: Request) {}
// If `OPTIONS` is not defined, Next.js will automatically implement `OPTIONS` and set the appropriate Response `Allow` header depending on the other methods defined in the route handler.
export async function OPTIONS(request: Request) {}White-tailed TropicbirdOP
I'l read that, thanks a lot