POST request 404 (Not Found) when deployed in Netlify

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;

Can you attach a link to your site or your site name/ID so I can look into this?

Did you see this guide yet? This is the best place to start for “Page Not Found” issues.

If you are still seeing issues after following the troubleshooting steps in there, please respond.

Thank you for the response.

Yes I saw that guide but unfortunately that is not related to the issue.

Here’s the test link for the site: empowered BI

You can replicate the issue once you filled out the fields and click submit.

Uploading: chrome_QK6DAhKA66.png…

Hi, @kevinsoma. There is no node.js application server at Netlify after deployment. This means you cannot run any server-side node.js code unless you deploy it as a function. The site you mention has no deployed functions here:

https://app.netlify.com/sites/sparkly-narwhal-930b72/functions

So, as you have no server-side code running at all, I would expect that sending emails would fail. You have not deployed the code that sends the emails in a way that will work.

There is a support guide about this below:

Can the email code be ported to a function? If so, have you done so?

Hi, @luke . Thank you so much for the detailed explanation, I was more enlightened to find what to do to fix it.

To make it work I needed to deploy the project to our test server as there is node.js application there. All good.

Awesome glad to hear you found your solution!