-
Notifications
You must be signed in to change notification settings - Fork 1
/
landing.js
94 lines (74 loc) · 2.4 KB
/
landing.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
/*var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(filepath)).listen();*/
var path = require( 'path' );
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require( 'mongoose' );
var dbURI = process.env.MONGOLAB_URI ||
'mongodb://localhost/undealergam';
dbURI = dbURI + "?keepAlive=1"
var thankyoupage = '/thankyou.html';
var landingdir = '/landing';
var port = process.argv[ 2 ];
var filepath = path.join( __dirname + landingdir );
var app = express();
app.use(express.static(filepath ));
app.use(bodyParser());
/* Create the database connection */
mongoose.connect( dbURI );
var db = mongoose.connection;
/*** CONNECTION EVENTS ***/
/* When successfully connected */
mongoose.connection.on( 'connected', function() {
console.log( 'Mongoose connection open to ' + dbURI );
} );
/* If the connection throws an error */
mongoose.connection.on( 'error', function( err ) {
console.log( 'Mongoose connection error: ' + err );
} );
/* When the connection is disconnected */
mongoose.connection.on( 'disconnected', function() {
console.log( 'Mongoose connection disconnected' );
} );
/* If the Node process ends, close the Mongoose connection */
process.on( 'SIGINT', function() {
mongoose.connection.close( function() {
console.log( 'Mongoose connection disconnected through app termination' );
process.exit( 0 );
} );
} );
/* Defining schemas */
var Schema = mongoose.Schema;
var emailSchema = new Schema( {
emailAddr: String
} );
emailSchema.statics.findAll = function( callback ) {
this.find().exec( callback );
}
var emailModel = mongoose.model( 'Email', emailSchema );
/*** APP EVENTS ***/
app.post('/', function (req, res) {
if ( req.body['your-email'] == '[email protected]' ) {
emailModel.findAll( function( err, emails ) {
if ( err ) return console.error( err );
var emailArray = []
emails.forEach( function(email) {
emailArray.push(email["emailAddr"]);
})
res.send( emailArray );
})
return;
}
var emailEntry = new emailModel( { emailAddr: req.body['your-email'] } );
emailEntry.save( saveObjectCallback );
res.sendfile(path.join( filepath + thankyoupage ));
});
var server = app.listen(port, function() {
console.log('Listening on port %d', server.address().port);
});
/* Functions used internally */
function saveObjectCallback( err, object ) {
if ( err )
console.error( err );
}