-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (52 loc) · 1.72 KB
/
server.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const pdf = require("html-pdf");
const pdfTemplate = require("./documents");
const sgMail = require("@sendgrid/mail");
//[INSERT SENDGRID API KEY - YOU NEED TO STORE THIS IN A .ENV FILE TO AVOID CORS ISSUES]
const SENDGRID_API_KEY = 'SEND GRID API OR GET FROM process.env.SENDGRID_API_KEY';
sgMail.setApiKey(SENDGRID_API_KEY);
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// console.log that your server is up and running
app.listen(port, () => console.log(`Listening on port ${port}`));
// create a GET route to check if connected
app.get("/express_backend", (req, res) => {
res.send({ express: "EXPRESS BACKEND IS CONNECTED TO REACT" });
});
app.post("/create-pdf", (req, res) => {
pdf.create(pdfTemplate(req.body), {}).toFile("Company_NameStartForm.pdf", err => {
if (err) {
return console.log("error");
}
app.post("/send");
//could do a async /await on the client side also
res.send(Promise.resolve());
});
});
app.get("/fetch-pdf", (req, res) => {
res.sendFile(`${__dirname}/Company_NameStartForm.pdf`);
});
app.post("/send-email", (req, res) => {
console.log(req.body)
let emailData = req.body;
const msg = {
to: emailData.to,
from: emailData.from,
subject: emailData.subject,
html: pdfTemplate(emailData.content),
};
sgMail.send(msg, function(error, response) {
if (error) {
console.log(error);
} else {
res.send({ express: "Email was sent" });
res.send(Promise.resolve());
console.log("Success | " + response);
}
});
});