NuxtN
Nuxt7mo ago
22 replies
Sovereign

API doesnt work

server/api/auth/register.post.ts
import { User } from "~/models/user.model";
import rateLimit from "../../../server/middleware/rateLimit";

export default defineEventHandler(async (event) => {
  await rateLimit(event);

  const body = await readBody(event);
  const { username, email, password } = body;

  if (!username || !email || !password) {
    throw createError({ statusCode: 400, statusMessage: "Missing fields" });
  }

  if (await User.exists({ $or: [{ username }, { email }] })) {
    throw createError({
      statusCode: 409,
      statusMessage: "Username or email already exists",
    });
  }

  const user = await User.create({
    username,
    email,
    password,
    role: "user",
  });

  return {
    status: "ok",
    user: { id: user._id, username: user.username, email: user.email },
  };
});


I made a request to it and I just get index page

curl -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{"username":"sovereign","email":"sovrign@proton.me","password":"secret"}'


I followed https://nuxt.com/docs/guide/directory-structure/server and couldnt understand what I am doing wrong?
Nuxt
The server/ directory is used to register API and server handlers to your application.
server · Nuxt Directory Structure v3
Was this page helpful?