-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (58 loc) · 1.61 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
// server.js
// where your node app starts
// init project
var express = require('express'),
mongoModel = require('./model.js'),
bodyParser = require('body-parser'),
form = require('express-form'),
field = form.field
var app = express();
app.use(bodyParser());
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
app.get("/", function (request, response) {
response.sendFile(__dirname + '/views/index.html');
})
app.get("/songs", function (request, response) {
mongoModel.find('songs',
function(data) {
response.send(data);
}
);
});
app.delete("/songs/:id", function(request, response){
var id = request.params.id;
console.log('Deleting song: '+ id)
mongoModel.delete('songs', id, function(data){
console.log('deleted ' + id + ':'+ data)
});
})
app.post("/songs",
form(
field("artist").trim().required(),
field("song").trim().required()
),
function(request, response){
if (!request.form.isValid) {
// Handle errors
response.send(request.form.errors);
} else {
// Or, use filtered form data from the form object:
console.log("artist:", request.form.artist);
console.log("song:", request.form.song);
mongoModel.save('songs',
{
'artist' : request.form.artist,
'song' : request.form.song
},
function(data){
response.send(data.ops[0]);
}
)
}
}
)
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});