-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
124 lines (102 loc) · 2.92 KB
/
server.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// require express and other modules
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
// configure bodyParser (for receiving form data)
app.use(bodyParser.urlencoded({ extended: true }));
// serve static files from public folder
app.use(express.static(__dirname + '/public'));
// set view engine to hbs (handlebars)
app.set('view engine', 'hbs');
// connect to mongodb
mongoose.connect('mongodb://localhost/spotify-app');
// require Track model
var Track = require('./models/track');
// HOMEPAGE ROUTE
app.get('/', function (req, res) {
res.render('index');
});
// API ROUTES
// get all tracks
app.get('/api/tracks', function (req, res) {
// find all tracks in db
Track.find(function(err, allTracks){
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ tracks: allTracks });
}
});
});
// create new track
app.post('/api/tracks', function (req, res) {
// create new track with form data (`req.body`)
var newTrack = new Track(req.body);
// save new track in db
newTrack.save(function(err, savedTrack){
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json(savedTrack);
}
});
});
// get one track
app.get('/api/tracks/:id', function (req, res) {
// get track id from url params (`req.params`)
var trackId = req.params.id;
// find track in db by its id
Track.findOne({ _id: trackId }, function(err, foundTrack){
if(err){
if(err.name === "CastError"){
res.status(404).json({ error: "Nothing found by this ID." });
} else {
res.status(500).json({ error: err.message });
}
} else {
res.json(foundTrack);
}
});
});
// update track
app.put('/api/tracks/:id', function (req, res) {
// get track id from url params (`req.params`)
var trackId = req.params.id;
// find track in db by its id
Track.findOne({ _id: trackId }, function(err, foundTrack){
if(err) {
res.status(500).json({ error: err.message });
} else {
//update the track's attributes
foundTrack.title = req.body.title;
foundTrack.artist = req.body.artist;
foundTrack.description = req.body.description;
// save updated track in db
foundTrack.save(function(err, savedTrack){
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json(savedTrack);
}
});
}
});
});
// delete track
app.delete('/api/tracks/:id', function (req, res) {
// get track id from url params (`req.params`)
var trackId = req.params.id;
// find track in db and remove
Track.findOneAndRemove({ _id: trackId }, function(err, deletedTrack){
if(err){
res.status(500).json({ error: err.message });
} else {
res.json(deletedTrack);
}
});
});
// listen on port 3000
app.listen(3000, function() {
console.log('server started');
});