-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (66 loc) · 1.7 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
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
import env from "dotenv";
const app = express();
const port = 5000;
env.config();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
const db = new pg.Client({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
});
db.connect();
app.get("/", async (req, res) => {
try {
const result = await db.query("SELECT * FROM posts ORDER BY date DESC");
const posts = result.rows;
res.render("index.ejs", {
posts: posts,
content: "",
});
} catch (err) {
console.log(err);
res.render("index.ejs", {
posts: [],
content: "You have not created any posts yet!",
});
}
});
app.get("/", (req, res) => {
res.render("index.ejs", {
content: "You have not created any posts yet!",
});
});
app.post("/newPost", (req, res) => {
res.render("newPost.ejs");
});
app.post("/", async (req, res) => {
let title = req.body["title"];
let content = req.body["content"];
let author = req.body["author"];
console.log(title, content, author);
try {
await db.query(
"INSERT INTO posts (title, content, author) VALUES ($1, $2, $3)",
[title, content, author],
(err, result) => {
if (err) {
console.log(err);
} else {
console.log("Post added successfully");
}
}
);
res.redirect("/");
} catch (err) {
console.log(err);
}
});
app.listen(port, () => {
console.log(`Listening at port ${port}`);
});