-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (92 loc) · 2.54 KB
/
index.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
import express from 'express';
import pkg from 'pg';
import {
postPW,
postHost,
postDB,
postUser,
postPort,
secretToken,
} from './config.js';
const { Pool } = pkg;
const app = express();
// Need to use this process.env.PORT for Render to work.
const port = process.env.PORT || 3000;
const server = app.listen(process.env.PORT || 3000, () => {
console.log(`App running on port http://localhost:${port}`);
});
const pool = new Pool({
user: process.env.POSTUSER || postUser,
host: process.env.POSTHOST || postHost,
database: process.env.POSTDB || postDB,
password: process.env.POSTPW || postPW,
port: process.env.POSTPORT || postPort,
ssl: {
rejectUnauthorized: false,
},
});
// TODO: Test and implement endpoint in frontend!
// Returns all settings
app.get(`/${secretToken}/settings`, async (req, res) => {
try {
const result = await pool.query('SELECT * FROM "Settings"');
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Error querying database');
}
});
// TODO: Test and implement endpoint in frontend!
// Returns all featured items.
app.get(`/${secretToken}/featured`, async (req, res) => {
try {
const result = await pool.query('SELECT * FROM "Featured"');
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Error querying database');
}
});
// Returns all users.
// TODO: Test and implement endpoint in frontend!
app.get(`/${secretToken}/users`, async (req, res) => {
try {
const result = await pool.query('SELECT * FROM "Users"');
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Error querying database');
}
});
// Returns all publics users
app.get(`/${secretToken}/public-users`, async (req, res) => {
try {
const result = await pool.query(
'SELECT * FROM "Settings" WHERE "isPublic" = true'
);
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Error querying database');
}
});
// Returns true or false if user exists.
// TODO: Test and implement endpoint in frontend!
app.get(`/${secretToken}/users/:userId`, async (req, res) => {
const userId = req.params.userId;
try {
const result = await pool.query(
'SELECT * FROM "Users" WHERE "userId" = $1',
[userId]
);
if (result.rows.length > 0) {
res.json({ exists: true });
} else {
res.json({ exists: false });
}
} catch (err) {
console.error(err);
res.status(500).send('Error querying database');
}
});
export { app, server };