-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
86 lines (73 loc) · 2.19 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
75
76
77
78
79
80
81
82
83
84
85
86
var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;
var DISHES_COLLECTION = "dishes";
var app = express();
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
var db;
console.log("this is connection url: ", process.env.MONGODB_URI);
mongodb.MongoClient.connect(process.env.MONGODB_URI, function (err, database) {
if (err) {
console.log(err);
process.exit(1);
}
db = database;
console.log("Database connection ready");
var server = app.listen(process.env.PORT || 8080, function() {
var port = server.address().port;
console.log("App now running on port", port);
});
});
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
res.status(code || 500).json({"error": message});
}
app.get("/dishes", function(req, res) {
db.collection(DISHES_COLLECTION).find({}).toArray(function(err, docs) {
if (err) {
handleError(res, err.message, "Failed to get contacts.");
} else {
res.status(200).json(docs);
}
});
});
//-------------CONSTRUSCTION-------
app.post("/postDishes", function(req, res, next) {
// res.send(req.params.restrictions)
var filteredResult = db
.collection(DISHES_COLLECTION)
.find({ tags: {$all: req.body.restriction} }).toArray(function(err, docs) {
console.log("----YYOO this is req.body: ", req.body)
res.status(200).json(docs);
})
})
//---------------------------------
app.post("/dishes/:restrictions", function(req, res) {
var filterByRestrictions = req.body.restrictions;
// newDish.createDate = new Date();
if (!(req.body.name || req.body.photoUrl)) {
handleError(res, "Invalid user inpput", "Must provide a first or last name.", 400);
}
db.collection(DISHES_COLLECTION).insertOne(newDish, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to create new dish");
} else {
res.status(201).json(doc.ops[0])
}
})
});
//
// app.get("/dishes/:id", function(req, res) {
//
// });
//
// app.put("/dishes/:id", function(req, res) {
//
// });
//
// app.delete("/dishes/:id", function(req, res) {
//
// });