-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (55 loc) · 1.48 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
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/blogroll');
var Schema = mongoose.Schema;
var BlogSchema = new Schema({
author: String,
title: String,
url: String
});
mongoose.model('Blog',BlogSchema);
var Blog = mongoose.model('Blog');
/*var blog = new Blog({
author: 'Michael',
title: 'Michel\'s Blog',
url: 'http://michaelblog.com'
});
blog.save();*/
var app = express();
app.use(bodyParser.json());
app.use(express.static(__dirname+'/public'));
// ROUTES
app.get('/api/blogs',function(req,res){
Blog.find(function(err,docs){
docs.forEach(function(item){
console.log('Received a GET REQUEST FOR ID'+item._id);
});
res.send(docs);
});
});
app.post('/api/blogs', function(req, res){
console.log('Received a Post request');
for (var key in req.body){
console.log(key + ': ' + req.body[key]);
}
var blog = new Blog(req.body);
blog.save(function(err, doc){
res.send(doc);
});
});
app.delete('/api/blogs/:id', function(req, res){
console.log('Received a DELETE request for _id: '+req.params.id);
Blog.remove({_id: req.params.id}, function(err,doc){
res.send({_id: req.params.id});
});
});
app.put('/api/blogs/:id', function(req, res){
console.log('Received a UPDATE request for _id: '+req.params.id);
Blog.update({_id: req.params.id}, req.body, function(err){
res.send({_id: req.params.id});
});
});
var port = 3000;
app.listen(port),
console.log('server on'+ port);