forked from Ktsiang18/MyNomz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
141 lines (125 loc) · 3.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var express = require('express')
var path = require('path')
var app = express()
var bodyParser = require('body-parser')
var routes = require('./routes')
var server = require('http').createServer(app);
app.use('/', routes)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(express.static(__dirname + '/public'));
var mongoose = require('mongoose');
mongoose.connect("mongodb://kmt2288:Mauipuprox18@ds113522.mlab.com:13522/nomzdata");
var db = mongoose.connection;
//GET LOGIN PAGE
app.get('/', function (req, res, next) {
res.sendFile(path.join(__dirname, '/public/nomzStuff', 'logIn.html'))
});
//CREATE NEW USER
var userSchema = new mongoose.Schema({
name: String,
lat: Number,
lon: Number,
req_allergies: String,
req_servings: Number,
req_radius: Number,
donate_servings: Number,
donate_type: String
});
var User = mongoose.model('User', userSchema);
app.post('/', function (req, res) {
var u = new User({
name: req.body.uname,
lat: null,
lon: null,
req_allergies: null,
req_servings: null,
req_radius: null,
donate_servings: null,
donate_type: null
});
u.save(function (err) {
if (err)
throw err;
else
console.log("user save successfully: ", u);
res.redirect('/home/' + u._id);
});
});
//POST LOCATION TO MONGODB;
app.post('/home/:id', function (req, res) {
var options = req.body;
User.findOneAndUpdate({
'_id': req.params.id
}, {
$set: {
lat: options.lat,
lon: options.lon
}
}, {
upsert: true
}, function (err, doc) {
if (err) return res.status(500).send({
error: err
});
else
console.log("doc: ", doc);
});
console.log("successfully saved location");
});
//POST REQUEST TO DB
app.post('/request/:id', function (req, res){
console.log("req.body", req.body);
User.findOneAndUpdate({
'_id': req.params.id
}, {
$set: {
req_allergies: req.body.allergies,
req_servings: req.body.servings,
req_radius: req.body.radius
}
}, function (err) {
if (err)
throw err;
else
console.log("successfully recorded food request");
});
});
//POST DONATION TO DB
app.post('/donate/:id', function (req, res){
console.log("req.body", req.body);
User.findOneAndUpdate({
'_id': req.params.id
}, {
$set: {
donate_servings: req.body.donate_servings,
donate_type: req.body.donate_type
}
}, function (err) {
if (err)
throw err;
else
console.log("successfully recorded food request");
});
});
//GET LOCATIONS AND REQUESTS/DONATIONS
app.get('/home/:type/:id', function(req, res){
if(req.params.type == 'request'){
var fields = 'name req_allergies req_servings req_radius';
var query = {req_servings: {$ne:null}};
}
else {
var fields = 'name donate_servings donate_type';
var query = {donate_type: {$ne: null}};
}
User.find(query, function(err, docs){
if(err) throw err;
console.log("data retrieve success: ", docs);
res.send(docs);
});
});
app.listen(process.env.PORT || 8000, function () {
console.log("Listening on port 8000")
})