-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (54 loc) · 1.53 KB
/
index.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 parser = require("body-parser");
var mongoose= require("./db/connection");
mongoose.Promise = global.Promise;
var cors = require('cors');
var app = express();
app.use(cors());
var Food = mongoose.model("Food");
var Mood = mongoose.model("Mood");
app.set("port", process.env.PORT || 3001);
app.use(parser.json({extended: true})); //to support JSON encoded-bodies
app.use(parser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.get("/api/moods", function(req, res){
Mood.find({}).then(function(moods){
res.json(moods);
});
});
app.get("/api/moods/:title", function(req, res){
Mood.findOne({title: req.params.title}).then(function(mood){
res.json(mood);
});
});
app.get("/api/foods", function(req, res){
Food.find({}).then(function(foods){
res.json(foods);
});
});
app.get("/api/foods/:title", function(req, res){
Food.findOne({title: req.params.title}).then(function(food){
res.json(food);
});
});
app.post("/api/foods", function(req, res){
Food.create(req.body).then(function(food){
res.json(food);
});
});
app.delete("/api/foods/:title", function(req, res){
Food.findOneAndRemove({title: req.params.title}).then(function(){
res.json({success: true });
});
});
app.put("/api/foods", function(req, res){
console.log("this is req.body: " + req.body);
Food.findOneAndUpdate({_id: req.body._id}, req.body)
.then(function(food){
res.json(req.body);
});
});
app.listen(app.get("port"), function(){
console.log("It's aliiive!");
});