-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (84 loc) · 2.27 KB
/
server.js
File metadata and controls
95 lines (84 loc) · 2.27 KB
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
const express = require("express");
const app = express();
const path = require("path");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const album = require("./Albums");
require("dotenv").config();
const port = process.env.PORT || 3000;
const uri = process.env.URI;
app.use(express.json());
app.use(bodyParser.json());
mongoose
.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("MongoDB is connected");
})
.catch((error) => console.error("Error in the connection", error));
app.use(express.static(path.join(__dirname, "public")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
app.get("/api/albums", async (req, res) => {
try {
const albums = await album.find();
console.log(albums);
res.json(albums);
} catch (error) {
res.status(500).json("Error in getting the albums");
}
});
app.post("/api/albums", async (req, res) => {
try {
const info = req.body;
const existingAlbum = await album.findOne({ title: info.title });
if (existingAlbum) {
return res.status(409).send({ message: "Album already exists" });
}
const newAlbum = new album({
title: info.title,
artist: info.artist,
year: info.year,
});
await newAlbum.save();
res.status(201).send(newAlbum);
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.put("/api/albums/:id", async (req, res) => {
try {
var id = req.params.id;
const albumToUpdate = req.body;
await album
.findByIdAndUpdate(id, albumToUpdate)
.then(() => {
res.sendStatus(200);
console.log("Updated");
})
.catch((error) => {
res.status(404).send({ status: "error", message: error });
});
} catch (error) {}
});
app.delete("/api/albums/:id", async (req, res) => {
try {
const id = req.params.id;
await album.findByIdAndDelete(id);
res.sendStatus(200);
} catch (error) {
res.status(404);
}
});
app.get("/api/albums/:title", async (req, res) => {
try {
const title = req.params.title;
await album.find({ title: title }).then((result) => {
res.status(200).json(result);
});
} catch (error) {
res.status(404);
}
});
app.listen(port);