-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (82 loc) · 2.09 KB
/
index.js
File metadata and controls
91 lines (82 loc) · 2.09 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
import express from "express";
import * as utils from "./utils/utils.js";
import * as db from "./utils/database.js";
import dotenv from "dotenv";
dotenv.config();
import cors from "cors";
let projects = [];
let contracts = [];
let mints = [];
const app = express();
app.use(cors());
const port = 3000;
app.set("view engine", "ejs");
app.use(express.json());
app.use(express.static("public"));
app.get("/", async (req, res, next) => {
await db
.connect()
.then(async () => {
// query the databse for project records
projects = await db.getAllProjects();
contracts = [];
mints = [];
projects.forEach((item) => {
contracts.push(item.contractAddress);
mints.push(0); // initializing parallel array
});
let featuredRand = Math.floor(Math.random() * projects.length);
res.render("index.ejs", {
featuredProject: projects[featuredRand],
contracts: contracts,
mints: mints,
projects: projects,
});
})
.catch(next);
});
app.get("/projects", (req, res) => {
res.render("projects.ejs", {
contracts: contracts,
mints: mints,
projects: projects,
});
});
app.get("/project/:id", (req, res) => {
let id = req.params.id;
if (id > projects.length) {
throw new Error("No project with that ID");
}
res.render("project.ejs", {
project: projects[id - 1],
contracts: contracts,
mints: mints,
projects: projects,
});
});
app.get("/contact", (req, res) => {
res.render("contact.ejs");
});
app.post("/mail", async (req, res) => {
await utils
.sendMessage(req.body.sub, req.body.txt)
.then(() => {
res.send({ result: "success" });
})
.catch(() => {
res.send({ result: "failure" });
});
});
app.use(async (err, req, res, next) => {
console.log(err);
let msg;
msg = err.message;
if (msg != "No project with that ID") {
msg =
"There was an internal error. Apologies. We are working on cleaning up the mess.";
}
res.render("error.ejs", { msg: msg });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});