Next.js Discord

Discord Forum

404 not found

Answered
`icey tea dev` posted this in #help-forum
Open in Discord
I'm making a request on the frontend to one of my api's, and I'm getting the following error:
page.tsx: POST http://localhost:3001/api/login 404 (Not Found)

This is my file structure

18 Replies

  const onSubmit: SubmitHandler<Inputs> = async (data) => {
    const { email, password } = data;
    const response = await fetch('/api/login', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ email, password }),
    });
    // handle response

    // TODO: success or error

  };
Wuchang bream
Set the path to /api/login/route.ts instead. Then call /api/login again to see if that fixes it.
@`icey tea dev` same error
Wuchang bream
Can you share your login.ts file?
"use server";
import type { NextApiRequest, NextApiResponse } from "next";

type ResponseData = {
  message: string;
};

"use server";
export default function handler(
  req: NextApiRequest,
  res: NextApiResponse<ResponseData>
) {
  console.log("backend hit");
  res.status(200).json({ message: "Hello from Next.js!" });
}
ahh alr, so this did technically fix it
I'm just getting a different error now
net::ERR_ABORTED 405 (Method Not Allowed)
@`icey tea dev` net::ERR_ABORTED 405 (Method Not Allowed)
Wuchang bream
yeah, you should export POST rather than a default function.
where is all this on their docs? I can't seem to find it
Answer
oh these are called route handlers...
mb new to this
thanks
@`icey tea dev` thanks
Wuchang bream
Happy to help!
lmfao this is why I couldn't find anything 🤣 I had this on page router