-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (47 loc) · 1.45 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
require("dotenv").config();
const express = require("express");
const path = require("path");
const connectDB = require("./database/db");
const Music = require("./model/Music");
const app = express();
let music = null;
connectDB();
const port = process.env.PORT || 3000;
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded());
app.get("/", async (req, res) => {
const playlist = await Music.find();
res.render("index", { playlist });
});
app.get("/getById/:id", async (req, res) => {
const id = req.params.id;
music = await Music.findById({ _id: id });
const playlist = await Music.find();
res.render("admin", { playlist, music });
});
app.get("/admin", async (req, res) => {
const playlist = await Music.find();
res.render("admin", { playlist, music: null });
});
app.post("/create", async (req, res) => {
const music = req.body;
await Music.create(music);
res.redirect("/");
});
app.post("/update/:id", async (req, res) => {
const newMusic = req.body;
await Music.updateOne({ _id: req.params.id }, newMusic);
res.redirect("/admin");
});
app.get("/delete/:id", async (req, res) => {
const id = req.params.id;
await Music.deleteOne({ _id: id });
res.redirect("/admin");
});
app.get("/authBloq", (req, res) => {
res.send({message: "Página de administração bloqueada no momento"});
});
app.listen(port, () =>
console.log(`Servidor rodando em http://localhost:${port}`)
);