-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (54 loc) · 1.63 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
var express = require('express');
var app = express();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', { useMongoClient: true });
var db = mongoose.connection;
var bodyParser = require('body-parser');
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function(){
console.log('Successfully connected to db');
});
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
var userSchema = mongoose.Schema({
username: String,
img: String
});
var tweetSchema = mongoose.Schema({
message: String,
datetime: Date
})
var User = mongoose.model('User', userSchema);
var Tweet = mongoose.model('Tweet', tweetSchema);
app.get('/user', function(req, res){
console.log("I received user request");
var faris = new User({username: 'Faris Karcic', img: 'img/user.png'});
User.find(function(err, docs){
console.log(faris);
})
res.json(faris);
});
app.get('/tweets', function(req,res){
console.log("I received tweets request");
Tweet.find(function(err, docs){
console.log(docs);
res.json(docs);
});
});
app.post('/tweets', function(req, res){
req.body._id=0;
var tweet = new Tweet({message: req.body.message, datetime: Date()});
tweet.save(function(err,doc){
if (err) return console.error(err);
res.json(doc);
})
});
app.delete('/tweets/:id', function(req,res){
var id = req.params.id;
console.log(id);
Tweet.findByIdAndRemove(id, function(err, doc){
res.json(doc);
});
});
app.listen(4000);
console.log("Server running on port 4000");