I have created a static website with html, tailwindcss, and vanilla javascript. I am trying to create a simple POST request on the client side to the server-less function from Vercel. I deployed this on Vercel.
I keep getting a 404 not found error every time I fire the client side api. It never goes to the server-less side. I followed Vercel documentation by housing the server-less function inside an api folder.
Client-Side /dist/index.js
async function handleForm() {
let name = encodeInput(document.getElementById("floating_name").value);
let email = encodeInput(document.getElementById("floating_email").value);
let budget = encodeInput(document.getElementById("floating_budget").value);
let description = encodeInput(document.getElementById("floating_description").value);
try {
const formData = { name, email, budget, description };
console.log("formData", formData)
const response = await fetch("/api/form", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
});
const data = await response.text();
console.log("data", data);
} catch (error) {
console.error('Error:', error);
}
My server-less function /api/form
module.exports = async (req, res) => {
try {
const formData = req.body;
console.log("Received form data:", formData);
res.status(200).json({ success: true });
} catch (error) {
console.error('Error processing the request:', error.message);
// Set an error status code and send an error response
res.status(500).json({ error: 'Internal Server Error' });
}
};