forked from OriLev/State-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (50 loc) · 1.67 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
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
mongoose.connect(process.env.MONGOLAB_RED_URI ||'mongodb://localhost/rereddit-final');
var Question = require("./questionModel");
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false})); // To understand "why?"
app.use(express.static('Public'));
app.use(express.static('node_modules'));
app.get('/', function(req, res) {
res.sendFile(__dirname + "/index.html");
});
// app.get('/', function(req,res) {
// res.send("You are inside the fullstack project")
// });
app.get('/questions', function (req, res) {
res.json({beers: [
{ name: '512 IPA', style: 'IPA', image_url: 'http://bit.ly/1XtmB4d', abv: 5 },
{ name: '512 Pecan Porter', style: 'Porter', image_url: 'http://bit.ly/1Vk5xj4', abv: 4 }
]});
});
app.get('/questions', function(req, res) {
Question.find(function(error, questions) {
res.send(questions);
});
});
app.post('/questions', function(req, res, next){
var question = new Question(req.body);
question.save(function(err, question) {
if (err) { return next(err); }
res.json(question);
})
});
app.put('/questions/:_id', function(req, res, next){
Question.update({_id: req.params._id}, {$push: {"answers": req.body}}, function(error, question){
res.json(question);
});
});
app.delete('/questions/:id', function(req, res) {
console.log(req.params.id);
Question.findByIdAndRemove(req.params.id, function(err, data) {
if(err) {
console.log(err);
return next(err);
}
res.send(data);
});
});
app.listen(process.env.PORT || '4000');