Hi guys.
I’m having a 404 (Not Found) error when I’m trying to send email. I use Sendgrid to send emails, and I use NodeJS as a server. This is a React (Vite) Project btw.
Everything works fine on local but when I deployed it on Netlify only I’m getting this issue.
Vite Config
const { PORT = 3001 } = process.env;
export default defineConfig({
plugins: [react()],
server: {
proxy: {
'/api': {
target: `http://localhost:${PORT}`,
changeOrigin: true,
},
},
},
build: {
outDir: 'dist/app',
},
assetsInclude: ['**/*.PNG', '**/*.JPG'],
});
Router
router.post("/sendemail", (_req, res) => {
const { name, company, email, message } = _req.body;
const from = "senderemail@test.com";
const to = "receiveremail@test.com";
const subject = "New Contact Request";
const output = `
<p>You have a new Contact Request - April 28, 2023</p>
<h3>Contact Details</h3>
<ul>
<li>Full Name: ${name}</li>
<li>Company: ${company}</li>
<li>Email: ${email}</li>
<li>Message: ${message}</li>
</ul>
`;
sendEmail(to, from, subject, output);
});
“sendEmail” function
require("dotenv").config();
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey("API Key Here");
const sendEmail = (to, from, subject, text) => {
const msg = {
to,
from,
subject,
html: text,
};
sgMail.send(msg, function (err, result) {
if (err) {
console.log("Email Not Sent Error Occured");
} else {
console.log("Email was Sent");
}
});
};
module.exports = sendEmail;