-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (76 loc) · 2.55 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
var express = require("express"),
url = require('url'),
mongojs = require("mongojs"),
db = mongojs("cookbook", ["ingredients", "recipes", "recipesIngr"]),
app = express(),
bodyParser = require('body-parser'),
errorHandler = require('errorhandler'),
methodOverride = require('method-override'),
hostname = process.env.HOSTNAME || 'localhost',
port = parseInt(process.env.PORT, 10) || 4567,
publicDir = __dirname + '/public';
app.get("/", function (req, res) {
res.redirect("/index.html");
});
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(publicDir));
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
app.get('/recipe', function(req, res){
var url_parts = url.parse(req.url, true),
query = url_parts.query;
if(query.id){
resObject = db.recipes.findOne({ _id:mongojs.ObjectId(query.id) }, function(err, doc){
res.end(JSON.stringify(doc));
/*var recipe = doc;
recipe.Ingredients = [];
db.recipesIngr.find({"recId" : recipe.Id}, function(err, docx){
console.log(docx);
recipe.Ingredients = docx;
res.end(JSON.stringify(recipe));
})*/
//
}.bind(this));
//console.log("resObject " + resObject);
/*resObject = {
Id: query.id,
Title: "Морковка с яйцом и сахаром",
Image: "http://loremflickr.com/320/240",
Instructions: "Порубайте морковку, залейте яйцом и посыпьте сахаром.",
Ingredients: [
{
Name: "Морковка",
Amount: "4",
AmountType: "шт."
},
{
Name: "Сахар",
Amount: "100",
AmountType: "г."
},
{
Name: "Яйцо",
Amount: "4",
AmountType: "шт."
}
]
};*/
} else{
resObject = {}
}
//console.log(query.id, resObject);
});
app.get('/recipes', function(req, res){
db.recipes.find({}, function(err, docs){
console.log(docs.length);
res.end(JSON.stringify(docs));
});
});
console.log("Simple static server showing %s listening at http://%s:%s", publicDir, hostname, port);
app.listen(port, hostname);