-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
81 lines (72 loc) · 2.05 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
(function(){
var express = require('express')
, app = express()
, path = require('path')
, bodyParser = require('body-parser')
, server = require('http').createServer(app)
, mongoose = require('mongoose')
, uriUtil = require('mongodb-uri')
var port = process.env.PORT || 5000;
server.listen(port);
app.use('/', express.static(path.resolve('./public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
mongourl = process.env.MONGOLAB_URI || 'mongodb://localhost/quizapp';
var mongooseUri = uriUtil.formatMongoose(mongourl);
mongoose.connect(mongooseUri);
var EmailSchema = new mongoose.Schema({
email: {type: String, required: true, unique: true},
type: {type: String, required: true},
offer: {type: String},
region: {type: String},
timestamp: {type: Number, 'default': Date.now()}
});
var EmailModel = mongoose.model('Email', EmailSchema);
app.get('/', function(req, res){
res.sendfile('./index.html');
});
app.get('/wantEmails', function(req, res){
EmailModel.find({type: 'want'}, function(err, email){
//console.log(email);
res.json(email);
//res.json({hurr:'durr'});
});
});
app.get('/haveEmails', function(req, res){
EmailModel.find({type: 'have'}, function(err, email){
//console.log(email);
res.json(email);
//res.json({hurr:'durr'});
});
});
app.post('/wantEmails', function(req, res){
var email = new EmailModel({
email: req.body.email,
offer: req.body.offer,
region: req.body.region,
type: 'want'
});
email.save(function(err){
if(err) console.error(err);
res.redirect('/');
});
});
app.post('/haveEmails', function(req, res){
var email = new EmailModel({
email: req.body.email,
offer: req.body.offer,
region: req.body.region,
type: 'have'
});
email.save(function(err){
if(err) console.error(err);
res.redirect('/');
});
});
var pruneOldEmails = function(){
EmailModel.remove({timestamp: {$lte: Date.now() - 1800000}}, function(err, docs){
console.log('emails pruned:', docs);
});
};
setInterval(pruneOldEmails, 1800000);
})();