Next.js Discord

Discord Forum

why doesn't my api routes work

Answered
Golden northern bumble bee posted this in #help-forum
Open in Discord
Golden northern bumble beeOP
Answered by Asian black bear
View full answer

28 Replies

Asian black bear
signup must be a folder containing a route.ts with the code.
Golden northern bumble beeOP
ohhhhhhhhhhhh
so no /auth/signup.ts but /auth/signup/route.ts?
Asian black bear
A file /auth/signup.ts is not routeable.
Golden northern bumble beeOP
like this?
Asian black bear
Yes.
Golden northern bumble beeOP
lemme see
also how do i change the route type to post
import { NextApiRequest, NextApiResponse } from 'next';
import { users } from '@/lib/schema';
import { eq } from 'drizzle-orm';
import { db } from '@/lib/db';


export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    if(req.method !== 'POST') {
        const { username, password, email, invite} = req.body;

        if(!username || !password || !email || !invite) {
            return res.status(400).json({
                message: 'All fields are mandatory.'
            })
        }

        try {
            const existingUser = await db.query.users.findFirst({
                where: eq(users.username, username)
            });

            if(existingUser) {
                return res.status(400).json({ message: 'User already exists.' })
            };

            const hashedPassword = await Bun.password.hash(password);

            res.status(200).send({
                h_pw: hashedPassword 
            })

        } catch (error) {
            res.status(501).send({
                message: error,
                data: {
                    u: username,
                    ik: invite,
                    e: email
                }
            })
        }
    }
};
Asian black bear
That code is outdated from the pages router.
Asian black bear
Answer
Golden northern bumble beeOP
hm
Asian black bear
Route handlers in the app router follow entirely different conventions.
Golden northern bumble beeOP
hmm
so what do i do
Asian black bear
You read the docs I linked, that's what you'll do.
Golden northern bumble beeOP
don't send me there boss
i hatedocs
I legit don't get whats wrong with my code
i updated the func name to POST
Asian black bear
The signature must be exactly export async function POST(request: Request) {}. Additionally, reading the request body and sending responses and status works using the Response interface and no longer via a second parameter.
Golden northern bumble beeOP
i fixed it
Golden northern bumble beeOP
@Asian black bear how do i get the readablestream
to work as an object
Asian black bear
You mean to return a stream to a file within the route handler?
Golden northern bumble beeOP
i have a readablestream and im trying to be able to read things inside it
like request's body
Golden northern bumble beeOP
nvm fixed