Next.js Discord

Discord Forum

trying to get a basic api functionality on a fresh nextjs project

Unanswered
English Lop posted this in #help-forum
Open in Discord
English LopOP
migs:~/nextjs$ npx create-next-app@latest
✔ What is your project named? … nodemailer
✔ Would you like to use TypeScript? … Yes
✔ Would you like to use ESLint? … Yes
✔ Would you like to use Tailwind CSS? …  Yes
✔ Would you like to use `src/` directory? …  Yes
✔ Would you like to use App Router? (recommended) …  Yes
✔ Would you like to customize the default import alias? … No
Creating a new Next.js app in /home/migs/nextjs/nodemailer.


file structure included. Tried with /pages/api/test.ts and /api/test.ts inside the src directory, and inside app

both test.ts files are identical:

import { NextApiRequest, NextApiResponse } from "next";

const handler = (req: NextApiRequest, res: NextApiResponse) => {
  console.log(req);
  return res.status(200).json({ name: "miggy" });
};

export default handler;


my fetch example:
"use client";
import { useEffect } from "react";

export default function Home() {
  const MakeFetch = async () => {
    try {
      const rawRes = await fetch("/api/test");
      console.log(rawRes);
      rawRes.status ? console.log(rawRes.status) : null;
      const res = await rawRes.json();
      console.log(res);
    } catch (err) {
      console.log(err);
    }
  };

  useEffect(() => {
    MakeFetch();
  }, []);

  return (
    <>
      <div>hi</div>
      <button onClick={MakeFetch}>refetch</button>
    </>
  );
}


I have tried await fetch("/api/test") await fetch("pages/api/test")
and a whole wide range of things I could think of.

At this point i'm wondering if its because I don't have proper request methods set up I.E. GET/POST, but it looks like from the docs, this should work.

Can someone please tell me why all i'm getting is 404 errors?

17 Replies

English LopOP
not sure if relevant: i want to make a nodemailer using nextjs api's that way I don't need an external node app to send emails.
just attempted await fetch("http://localhost:3000/api/test"); and await fetch("http://localhost:3000/pages/api/test"); still getting 404
The pages folder should not be inside the app folder
English LopOP
i tried outside, let me test again.
src/pages/api/foo.js
English LopOP
const rawRes = await fetch("http://localhost:3000/pages/api/test"); and tried const rawRes = await fetch("/pages/api/test"); both 404
including github for current file structure/setup
English LopOP
i have tried putting pages/api on the same level as src now i really feel like im losing my mind lol
Not /pages/api/test
“/pages” is not part of the url
English LopOP
      const res2 = await fetch("http://localhost:3000/api/test");
      const res3 = await fetch("/api/test");
      const res4 = await fetch("api/test");

all of these are now returning status 200. neat
i pulled pages into the ./ and then put it back in src/ on the same level as app saved tsconfig and now it works
what a weird experience, since i tried that 2 other times. i thought i was going crazy
thank you both 😀