-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (42 loc) · 1.31 KB
/
app.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 express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var router = express.Router();
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
router.route('/rsvp').post(function(req,res){
console.log("inside the post",req.body);
db.collection('rsvp').save(req.body, function(err, result) {
if (err) {
return res.json({"error":"404"});
}
console.log("request saved in db");
res.json({message:"recieved the RSVP request"});
});
});
router.route('/rsvp').get(function(req,res){
db.collection('rsvp').ensureIndex({'email' : 1}, {unique : true, dropDups : true});
db.collection('rsvp').find().toArray(function(err, results) {
if (err) {
return res.json({"error":"404"});
}
console.log(results);
res.json(results);
});
});
MongoClient.connect('mongodb://localhost:27017/RSVP', function(err, database){
if (err){
throw err;
}
console.log('Connected to the database.');
db = database;
var server = app.listen(3000, function () {
console.log('api listening on', server.address().port);
});
});
app.use('/api', router);