Leo
Leo
NNuxt
Created by Leo on 6/8/2024 in #❓・help
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,
};
}
});
2 replies