-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (72 loc) · 1.93 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
import express from "express";
import pg from "pg";
import dotenv from "dotenv";
import BadWordsNext from "bad-words-next";
import en from "bad-words-next/data/en.json" assert { type: "json" };
dotenv.config();
const { DATABASE_URL, PORT } = process.env;
const app = express();
const client = new pg.Client({
connectionString: DATABASE_URL,
});
const badWords = new BadWordsNext({ data: en });
await client.connect();
app.use(express.static("public"));
app.use(express.json());
app.get("/nmb", getAllPosts);
app.post("/nmb", submitPost);
app.delete("/nmb/:id", deletePost);
app.patch("/nmb/:id", patchPost);
async function getAllPosts(_, res, next) {
try {
const posts = await client.query(
"SELECT * FROM posts ORDER BY id DESC LIMIT 100"
);
res.json(posts.rows);
} catch (err) {
next(err);
}
}
async function submitPost(req, res, next) {
try {
const { currentUser, message } = req.body;
await client.query(
`INSERT INTO posts (username, message, created_at)
VALUES ($1, $2, CURRENT_TIMESTAMP)`,
[currentUser, badWords.filter(message)]
);
res.sendStatus(202);
} catch (err) {
next(err);
}
}
async function deletePost(req, res, next) {
try {
await client.query(`DELETE FROM posts WHERE id=$1`, [req.params.id]);
res.sendStatus(200);
} catch (err) {
next(err);
}
}
async function patchPost(req, res, next) {
try {
await client.query(`UPDATE posts SET message = $1 WHERE id=$2`, [
badWords.filter(req.body.message),
req.params.id,
]);
res.sendStatus(200);
} catch (err) {
next(err);
}
}
app.use("/", (req, res) => {
res.sendStatus(404);
});
app.use((err, _, res, next) => {
console.error(err);
res.sendStatus("500");
next();
});
app.listen(PORT, () => {
console.log(`Connected on port ${PORT}`);
});