-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.15 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
const express = require("express");
const app = express();
const handlebars = require("express-handlebars");
const bodyParser = require("body-parser");
const Post = require("./models/post");
// Configure Handlebars - Template Engine
app.engine("handlebars", handlebars({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
// Body Parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Routes
app.get("/", (req, res) => {
Post.findAll({ order: [["id", "DESC"]] }).then((posts) => {
res.render("home", { posts: posts });
});
});
app.get("/write", (req, res) => {
res.render("form");
});
app.post("/add", (req, res) => {
Post.create({
title: req.body.title,
content: req.body.content,
})
.then(() => res.redirect("/"))
.catch((err) => res.send("Unable to add post\n" + err));
});
app.get("/delete/:id", (req, res) => {
Post.destroy({ where: { id: req.params.id } })
.then(() => res.redirect("/"))
.catch((err) => res.send("Unable to delete post\n" + err));
});
app.listen(8081, () => {
console.log("Front-End running on port 8081!");
});