-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
41 lines (36 loc) · 1.32 KB
/
app.js
File metadata and controls
41 lines (36 loc) · 1.32 KB
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
const express = require("express");
const request = require("superagent");
const bodyParser = require("body-parser");
const mailsacAPIKey = ""; // Generated by mailsac. See https://mailsac.com/api-keys
const mailsacFromAddress = "web-application@mailsac.com";
const app = express();
const port = 3000;
// The static middleware will serve index.html at http://localhost:3000/
app.use(express.static("static"));
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/create-user", async (req, res) => {
const mailsacToEmailAddress = req.body.email;
try {
const confirmationMail = await request
.post("https://mailsac.com/api/outgoing-messages")
.set("Mailsac-Key", mailsacAPIKey)
.send({
to: mailsacToEmailAddress,
from: mailsacFromAddress,
subject: "Confirm your new example.com account",
text: "Visit https://example.com to confirm you account",
html:
"<div><a href='https://example.com'>Confirm you account</a></div>",
});
if (confirmationMail.status != 200) {
throw new Error(res.text);
}
res.redirect("/success.html");
} catch (err) {
res.status(err.status || 500);
res.send("Failed to send email using Mailsac API: " + err);
}
});
app.listen(port, () => {
console.log(`Server started at http://localhost:${port}`);
});