The file structure is correct.
You just need to use the full URL in the fetch request:
const res = await fetch("http://localhost:3000/api/contact", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});