-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
57 lines (48 loc) · 1.36 KB
/
app.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
var express = require("express");
var Post = require('./Post.js');
var app = express();
app.configure(function(){
app.use(express.urlencoded({limit: '3mb'}));
app.use(express.json({limit: '3mb'}));
app.use(app.router);
});
app.use('/', express.static(process.cwd() + '/public'));
app.get('/posts', function(req,res){
Post.find({}).sort({created: -1}).exec(function(err,docs){
if(err){ res.send('Error ' + err); return; }
res.send(docs);
});
});
app.get('/posts/:id', function(req,res){
Post.findById(req.params.id, function(err,doc){
if(err){ res.send('Error ' + err); return; }
res.send(doc);
});
});
app.post('/posts', function(req,res){
var postData = req.body;
var actualDate = Date.now();
postData.created = actualDate;
postData.updated = actualDate;
var newPost = new Post(postData);
newPost.save(function(err){
if(err){ res.send('Error ' + err); return; }
res.send(newPost);
});
});
app.put('/posts/:postid', function(req,res){
var postData = req.body;
var actualDate = Date.now();
postData.updated = actualDate;
Post.findByIdAndUpdate(req.params.postid, postData, function(err){
if(err){ res.send('Error ' + err); return; }
res.send('ok');
});
});
app.delete('/posts/:postid', function(req,res){
Post.findByIdAndRemove(req.params.postid, function(err){
if(err){ res.send('Error ' + err); return; }
res.send('ok');
});
});
app.listen(3000);