-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
93 lines (82 loc) · 3.01 KB
/
server.js
File metadata and controls
93 lines (82 loc) · 3.01 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
require('dotenv').config();
const path = require('path');
const express = require('express');
const mongoose = require('mongoose');
const mime = require('mime');
const Album = require('./models/model.js');
const app = express();
const port = process.env.PORT;
app.use(
express.json(),
express.static(path.join(__dirname, '.'))
);
// connect to MongoDB
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.log('Error connecting to MongoDB', err));
// serve index.html from src/html folder
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, './index.html'));
});
// get all albums - works
app.get('/api/albums', async (req, res) => {
const albums = await Album.find();
if (!albums) return res.status(404).send('No albums found');
res.json(albums);
});
// get album by title - works
app.get('/api/albums/:title', async (req, res) => {
if (!req.params.title) return res.status(400).send('Missing required fields');
try {
const album = await Album.findOne({ title: req.params.title });
if (!album) return res.status(404).send('Album not found');
res.json(album);
} catch (err) {
res.status(404).send(err.message);
}
});
// add new album - works
app.post('/api/albums', async (req, res) => {
// check if required fields are missing or empty
if (!req.body.title || !req.body.artist || !req.body.year || req.body.title === '' || req.body.artist === '' || req.body.year === '') return res.status(400).send('Missing required fields');
// look for duplicate album
const findDuplicate = await Album.findOne({ title: req.body.title, artist: req.body.artist, year: req.body.year });
if (findDuplicate) return res.status(409).send('Conflict: Album already exists');
try {
const newAlbum = await Album.create({
title: req.body.title,
artist: req.body.artist,
year: req.body.year
});
res.status(201).json(newAlbum);
} catch (err) {
res.status(404).send(err.message);
}
});
// update album - works
app.put('/api/albums/:id', async (req, res) => {
if (!req.params.id) return res.status(400).send('Missing required fields');
try {
const updatedAlbum = await Album.findByIdAndUpdate(req.params.id, {
title: req.body.title,
artist: req.body.artist,
year: req.body.year
}, { new: true });
if (!updatedAlbum) return res.status(404).send('Album not found');
res.status(201).json(updatedAlbum);
} catch (err) {
res.status(404).send(err.message);
}
});
// delete album - works
app.delete('/api/albums/:id', async (req, res) => {
if (!req.params.id) return res.status(400).send('Missing required fields');
try {
const deletedAlbum = await Album.findByIdAndDelete(req.params.id);
if (!deletedAlbum) return res.status(404).send('Album not found');
res.status(201).json(deletedAlbum);
} catch (err) {
res.status(404).send(err.message);
}
});
app.listen(port, () => console.log(`Server listening on port ${port}`));