-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (41 loc) · 1.32 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
const express = require('express');
const path = require('path');
const nodeMailer = require('nodemailer');
// const { user, pass } = require('./config/production.json');
require('dotenv').config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.post('/send', (req, res) => {
const { name, email: from, subject, message } = req.body;
const transporter = nodeMailer.createTransport({
service: 'gmail',
auth: {
user: process.env.ACCOUNT_EMAIL,
pass: process.env.ACCOUNT_PASS,
},
});
const mailOptions = {
from,
to: '[email protected]',
subject: `Email from ${from} about ${subject}`,
text: `I am ${name} and This is my message :\n${message}`,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return res.status(500).json({ error });
} else {
return res.status(200).json({ response: info.response });
}
});
});
// serve static assets in production
if (process.env.NODE_ENV === 'production') {
// set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server started on PORT ${PORT}...`));