N
Nuxt6mo ago
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,
};
}
});
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,
};
}
});
1 Reply
Want results from more Discord servers?
Add your server