-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
41 lines (33 loc) · 965 Bytes
/
Copy pathapi.js
File metadata and controls
41 lines (33 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import express from "express";
import path from 'path';
import sgMail from '@sendgrid/mail'
import * as dotenv from 'dotenv'
dotenv.config()
const app = express();
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
app.use(express.json())
app.use(express.static('app'))
app.get('/', (req, res) => {
res.sendFile(`${path.resolve()}/index.html`)
})
app.post('/send', async (req,res) => {
console.log(req.body);
const {to, subject, html} = req.body
const msg = {
to, // Change to your recipient
from: process.env.FROM, // Change to your verified sender
subject,
html,
}
try {
await sgMail.send(msg)
res.sendStatus(204)
} catch (error) {
console.log("ERROR!! # $&^ ", error);
const messages = error.response.body.errors.map(e => e.message).join(' ')
res.status(400).send(messages)
}
})
app.listen('3000', () => {
console.log("The App is READY!");
})