-
Notifications
You must be signed in to change notification settings - Fork 0
/
EpulaeService.js
61 lines (52 loc) · 1.99 KB
/
EpulaeService.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
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var schedule = require('node-schedule');
var LinvoDB = require("linvodb3");
LinvoDB.dbPath = process.cwd();
var Foods = new LinvoDB("doc", { /* schema, can be empty */ });
var util = require('util');
var path = require('path');
var allowCrossDomain = function(req,res,next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.use(allowCrossDomain);
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.use("/public", express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended:true}));
app.get('/',function(request,response){
response.sendFile('Epulae-Alpha.html', {"root": __dirname});
});
app.get('/downloadFoodItems',function(request,response){
console.log("New foods download request.");
Foods.find({}, function (err, docs) {
response.send(docs);
});
});
app.post('/addFoodItem',function(request,response){
console.log(request.body);
Foods.insert(request.body, function (err, newDoc) {
response.send({message:"New food item added: "+request.body.Name});
});
});
app.post('/editFoodItem',function(request,response){
Foods.findOne({ foodID: request.foodID }, function(err, doc) {
doc.save(function(err) { response.send({message:"Food updated successfully."})});
});
});
app.post('/deleteFoodItem',function(request,response){
var id = request.id;
Foods.remove({ id: id }, {}, function (err, numRemoved) {
console.log("Food removed successfully.");
response.send({message: "Food removed successfully: "+id})
});
});
var server = app.listen(1020, function(){
var host = server.address().address;
var port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port)
});