-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
88 lines (66 loc) · 2.07 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require("express");
const app = express();
const PORT = process.env.PORT || 8080;
const bodyParser = require("body-parser");
const cors = require("cors");
const nodemailer = require("nodemailer");
const sendgridTransport = require("nodemailer-sendgrid-transport");
// const SendEmail = require("./model/SendEmail");
require("dotenv").config();
// "proxy": "http://localhost:8080"
// "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
//db connection
// require("./model/db");
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//to send email
const transporter = nodemailer.createTransport(
sendgridTransport({
auth: {
api_key: process.env.API_SENDGRID,
},
})
);
app.post("/sendemail", (req, res) => {
const { name, email, servicetype, message } = req.body;
if (!name) {
return res.status(400).json({ error: "Please add your name" });
}
if (!email) {
return res.status(400).json({ error: "Please add your e-mail" });
}
if (!servicetype) {
return res.status(400).json({ error: "Please add what service you need" });
}
if (!message) {
return res.status(400).json({ error: "Please add your message" });
}
transporter.sendMail({
to: "[email protected]",
from: "[email protected]",
subject: "Contact Message",
html: `<h1>You have one new E-mail:
<h5>Your Details</h5>
<ul>
<li><p>Your Name: ${name}</p></li>
<li><p>Your E-mail: ${email}</p></li>
<li><p>Service Type: ${servicetype}</p></li>
<li><p>Message: ${message}</p></li>
</ul>
</h1>`,
});
res.json({ name });
});
//to deploy heroku
// Serve static assets if in production
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
app.listen(PORT, (req, res) => {
console.log("Server connected");
});