-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (60 loc) · 1.71 KB
/
server.js
File metadata and controls
71 lines (60 loc) · 1.71 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
var express = require('express')
var app = express()
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/tp8');
var bodyParser = require('body-parser');
var Album = require('./models/album');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = 8080;
app.get('/albums', function(req, res) {
res.setHeader('Content-Type', 'application/json');
Album.find(function(err, albums) {
if (err)
res.send(err);
res.json(albums);
})
});
//app.post('/addalbums', function(req, res) {
// res.setHeader('Content-Type', 'application/json');
//
// Album.create({title: "Holly Rolling",mc: "El Mega", price: 2000}, function (err, small) {
// if (err) res.send(err);
// res.json({ message: 'Successfully added' });
// })
//});
app.post('/albums', function(req, res) {
res.setHeader('Content-Type', 'application/json');
Album.create({title: req.body.title ,mc: req.body.mc ,price: req.body.price}, function (err, small) {
if (err) console.log(err);
res.json({ message: 'Successfully added' });
})
});
app.patch('/albums/:id', function(req, res) {
Album.findOneAndUpdate({
_id: req.params.id
},
{ $set: { price: req.body.price}
}, {upsert: true}, function(err, newAlbum) {
if (err) {
res.send('error updating ');
} else {
console.log(newAlbum);
res.send(newAlbum);
}
});
});
app.delete('/albums/:id', function(req, res) {
Album.findOneAndRemove({
_id: req.params.id
}, function(err, deleteAlbum) {
if(err) {
res.send('error removing')
} else {
console.log(deleteAlbum);
res.status(204);
res.send(deleteAlbum);
}
});
});
app.listen(port);