NuxtN
Nuxt2y ago
1 reply
Leo

Custom status code in server folder

hey in the server folder how can I set a custom response status code?

import authManager from "~/server/helpers/authManager";

export default defineEventHandler(async (event) => {
  const body = await readBody(event);
  if (!body.username || !body.email || !body.password)
    return {
      success: false,
      error: "Missing body parameters",
    };
  try {
    await authManager.register(body.username, body.email, body.password);
    // 201
    return {
      success: true,
    };
  } catch (error) {
    let message;
    if (error instanceof Error) message = error.message;
    else message = String(error);
    // 400
    return {
      success: false,
      error: message,
    };
  }
});
Was this page helpful?