I want to implement Sendgrid’s API to receive e-mail to a certain gmail account whenever someone submits the contact me form. The project I am on is made with react (for routing using react-router if that’s relevant) and express.
Then only thing that happens after submitting is “cannot POST /contanct”.
Feel lost with this, so some guidance on what I ma doing wrong will be really appreciated!
Here is the form :
export default () => (
<div className="contact">
<div className="contact__form">
<form method="post" action="/contact">
<input name="firstName" field="firstName" placeholder='Name' />
<input name="e-mail" field="e-mail" placeholder='E-mail' />
<input name="subject" field="subject" placeholder='Subject' />
<input name="message" field="message" placeholder='Message' />
<button type="submit">Submit</button>
</form>
</div>
</div>
);
And here is the server.js file :
const path = require('path');
const express = require('express');
const publicPath = path.join(__dirname, '../public');
const port = process.env.PORT || 3000;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const app = express();
app.use(express.static(publicPath));
app.post('/contact', (req, res) => {
const msg = {
to: 'name.surname@gmail.com',
from: 'name@inbox.lv',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
};
sgMail
.send(msg)
.then(() => console.log('Mail sent successfully'))
.catch(error => console.error(error.toString()))
});
app.get('*', (req, res) => {
res.sendFile(path.join(publicPath, 'index.html'));
});
app.listen(port, () => {
console.log('Server is up');
});