-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (97 loc) · 3.5 KB
/
Copy pathindex.js
File metadata and controls
120 lines (97 loc) · 3.5 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const config = require("./config.json");
//const mysql = require("mysql");
//const con = mysql.createConnection(config.database);
const express = require("express");
const app = express();
const nodemailer = require("nodemailer");
const multiparty = require("multiparty");
const mustacheExpress = require("mustache-express");
app.set("views", `${__dirname}/views`);
app.set("view engine", "mustache");
app.engine("mustache", mustacheExpress());
const transporter = nodemailer.createTransport({
host: "smtp.sendgrid.net",
port: 25,
auth: {
user: config.smtp.user,
pass: config.smtp.pass,
},
});
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
app.get("/", function (req, res) {
res.render("index", { pageTitle: "Home" });
});
app.get("/government/how-government-works", function (req, res) {
res.render("government/how-government-works", { pageTitle: "How government works" });
});
app.get("/government/who-represents-me", function (req, res) {
res.render("government/who-represents-me", { pageTitle: "Who represents me?" });
});
app.get("/government/behind-the-scenes", function (req, res) {
res.render("government/behind-the-scenes", { pageTitle: "Behind the scenes" });
});
app.get("/government/elections", function (req, res) {
res.render("government/elections", { pageTitle: "Presidential elections" });
});
app.get("/forms/sd01", function (req, res) {
res.render("forms/sd01", { pageTitle: "Declaration of Intent (SD01)" });
});
app.post("/forms/sd01", function (req, res) {
let ts = Date.now();
let date_ob = new Date(ts);
let date = date_ob.getDate();
let month = date_ob.getMonth() + 1;
let year = date_ob.getFullYear();
// prints date & time in YYYY-MM-DD format
console.log(year + "-" + month + "-" + date);
//1.
let form = new multiparty.Form();
let data = {};
form.parse(req, function (err, fields) {
console.log(fields);
Object.keys(fields).forEach(function (property) {
data[property] = fields[property].toString();
});
//2. You can configure the object however you want
const mail = {
from: 'Form Submissions <noreply@cloudcraft.fi>',
to: 'overseers@cloudcraft.fi',
cc: 'dandabs@cloudcraft.fi',
subject: 'SD01: ' + data.minecraft,
priority: 'high',
text: `SD01 from ${data.minecraft} | ${data.discord}.
Primary election color: ${data.color}.
Primary election animal: ${data.animal}.
Primary flag(s): ${data.flag}.
Mother tongue: ${data.language}\n
Why do you want to become president?\n${data.whydoyouwant}.\n
What is an important issue on CC, and how would you propose to solve it?\n${data.importantissue}.\n
What's something you'd like to see implemented on CloudCraft?\n${data.newfeature}.\n
Form submitted at ${date + "/" + month + "/" + year}.`,
};
//3.
transporter.sendMail(mail, (err, data) => {
if (err) {
console.log(err);
res.status(500).send("Something went wrong.");
} else {
//res.status(200).send("Email successfully sent to recipient!");
res.render("forms/sd01-sent", { pageTitle: "Declaration of Intent (SD01)" });
}
});
});
});
app.use("/cdn", express.static("content"));
app.use("/cdn/bootstrap", express.static("node_modules/bootstrap/dist"));
app.get('*', function(req, res){
res.status(404).send('what???');
});
app.listen(config.port, () => {
console.log(`Ready.`);
});