-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
154 lines (133 loc) · 3.36 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import express from "express"
import bodyParser from "body-parser"
import cors from "cors"
import dotenv from "dotenv"
import nodemailer from "nodemailer"
import pkg from "pg"
import path from "path"
import { fileURLToPath } from "url"
import { dirname } from "path"
const { Pool } = pkg
dotenv.config()
const app = express()
const { PORT, DB_USER, DB_HOST, DB_NAME, DB_PASSWORD, DB_PORT, DATABASE_URL } =
process.env
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
app.use(express.json())
app.use(bodyParser.json())
app.use(cors())
app.use(express.static(path.join(__dirname, "build")))
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"))
})
app.get("/contact", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"))
})
const pool = new Pool({
user: DB_USER,
host: DB_HOST,
database: DB_NAME,
password: DB_PASSWORD,
port: DB_PORT,
ssl: { rejectUnauthorized: false },
})
// const pool = new Pool({
// user: "daryletan",
// // host: "localhost",
// host: DATABASE_URL,
// database: "certforce",
// password: pw,
// port: 5432,
// ssl: { rejectUnauthorized: false },
// connectionTimeoutMillis: 20000,
// })
function sendEmail({ name, email, subject, message }) {
return new Promise((resolve, reject) => {
var transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.SMTP_MAIL,
pass: process.env.SMTP_PASSWORD,
},
})
var mailOptions = {
name: name,
from: email,
to: process.env.SMTP_MAIL,
subject: subject,
html: `
<p>${name}</p>
<p>${email}</p>
<p>${subject}</p>
<p>${message}</p>
`,
}
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error)
return reject({ message: `An error has occurred` })
}
return resolve({ message: "Email sent successfully" })
})
})
}
app.get("/sendEmail", (req, res) => {
sendEmail(req.query)
.then((response) => res.send(response.message))
.catch((error) => res.status(500).send(error.message))
})
app.get("/api/categories", (req, res, next) => {
pool
.query("SELECT * FROM categories;")
.then((response) => {
res.send(response.rows)
})
.catch(next)
})
app.get("/api/categories/:id", (req, res, next) => {
const id = req.params.id
pool
.query("SELECT * FROM categories WHERE id = $1;", [id])
.then((response) => {
const asset = response.rows[0]
if (asset) {
res.send(asset)
} else {
res.sendStatus(404)
}
})
.catch(next)
})
app.get("/api/clues", (req, res, next) => {
pool
.query("SELECT * FROM clues;")
.then((response) => {
res.send(response.rows)
})
.catch(next)
})
app.get("/api/clues/:id", (req, res, next) => {
const id = req.params.id
pool
.query("SELECT * FROM clues WHERE id = $1;", [id])
.then((response) => {
const asset = response.rows[0]
if (asset) {
res.send(asset)
} else {
res.sendStatus(404)
}
})
.catch(next)
})
app.use((err, req, res, next) => {
console.log(err)
return res
.set("content-type", "text/plain")
.status(500)
.send("Internal Server Error")
})
app.listen(PORT, () => {
console.log(`listening to ${PORT}`)
})