-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (71 loc) · 2.1 KB
/
Copy pathserver.js
File metadata and controls
84 lines (71 loc) · 2.1 KB
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');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var shortid = require('shortid');
var index = require('./routes/index');
var app = express();
var PORT = process.env.PORT || 3001;
var mongoURI = process.env.MONGOURI || 'mongodb://localhost/where2eat';
var mongoose = require('mongoose');
mongoose.connect(mongoURI);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Database Connection Successful");
});
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
var Event = require('./models/eventURLModel.js');
app.get('/votes', (req, res) => {
var id = shortid.generate();
var newEvent = new Event({
"_id": id,
"places": []
});
newEvent.save(function(err, newEvent) {
if (err) return console.error(err);
});
res.json(id);
});
app.post('/votes', (req,res) => {
var places = req.body['places[]'];
console.log(req.body.id)
Event.findOneAndUpdate({'_id': req.body.id}, {$set:{'places':places}}, function(err, doc){
if(err){
console.log("Something wrong when updating data!");
}
console.log(doc);
});
});
app.get('/votes/id', (req, res) => {
console.log(req.query.id)
Event.findOne({'_id':req.query.id}, 'places', function(err, places){
res.json(places)
if(err){
console.log("Something wrong when updating data!");
}
});
// var newEvent = new Event({
// "_id": id,
// "places": []
// });
// newEvent.save(function(err, newEvent) {
// if (err) return console.error(err);
// });
// res.json(id);
});
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, './client/build', 'index.html'));
});
app.listen(PORT, function() {
console.log("Running on port: ", PORT);
});
module.exports = app;