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 Giant panda
View full answer

28 Replies

Giant panda
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?
Giant panda
A file /auth/signup.ts is not routeable.
Golden northern bumble beeOP
like this?
Giant panda
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
                }
            })
        }
    }
};
Giant panda
That code is outdated from the pages router.
Giant panda
Answer
Golden northern bumble beeOP
hm
Giant panda
Route handlers in the app router follow entirely different conventions.
Golden northern bumble beeOP
hmm
so what do i do
Giant panda
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
Giant panda
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
@Giant panda how do i get the readablestream
to work as an object
Giant panda
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