-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
61 lines (52 loc) · 1.39 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
const fs = require(`fs`);
const path = require(`path`);
const util = require(`util`);
const express = require(`express`);
const bodyParser = require(`body-parser`);
const build = require("./lib/build");
const sendMail = require("./lib/send");
const app = express();
const port = process.env.PORT || 3001;
app.set("view engine", "ejs");
app.use(express.static(`dist`));
app.use(bodyParser.json());
app.get(`/`, async (req, res) => {
try {
const htmlFiles = await util.promisify(fs.readdir)(
path.join(__dirname, `dist`)
);
const templates = htmlFiles.map((file) => ({
file,
name: file.replace(`.html`, ``),
}));
res.render(`index`, { templates });
} catch (err) {
res.status(500).send(err.message);
}
});
app.get(`/build`, async (req, res) => {
try {
const { templateName, templateData } = req.body;
const html = await build(templateName, templateData);
res.json({ html });
} catch (err) {
res.status(500).send(err.message);
}
});
app.post(`/send`, async (req, res) => {
try {
const { mailgunApiKey, mailgunDomain, from, to, subject, html } = req.body;
const result = await sendMail(
mailgunApiKey,
mailgunDomain,
from,
to,
subject,
html
);
res.json({ result });
} catch (err) {
res.status(500).send(err.message);
}
});
app.listen(port, () => console.log(`Listening on ${port}`));