Next.js Discord

Discord Forum

always getting 404

Unanswered
White-tailed Tropicbird posted this in #help-forum
Open in Discord
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

"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 TropicbirdOP
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