-
Notifications
You must be signed in to change notification settings - Fork 99
/
server.js
84 lines (76 loc) · 2.83 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
//Copyright 2013-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//Licensed under the Apache License, Version 2.0 (the "License").
//You may not use this file except in compliance with the License.
//A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
//or in the "license" file accompanying this file. This file is distributed
//on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//either express or implied. See the License for the specific language
//governing permissions and limitations under the License.
//Get modules.
var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var fs = require('fs');
var AWS = require('aws-sdk');
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.locals.theme = process.env.THEME; //Make the THEME environment variable available to the app.
//Read config values from a JSON file.
var config = fs.readFileSync('./app_config.json', 'utf8');
config = JSON.parse(config);
//Create DynamoDB client and pass in region.
var db = new AWS.DynamoDB({region: config.AWS_REGION});
//Create SNS client and pass in region.
var sns = new AWS.SNS({ region: config.AWS_REGION});
//GET home page.
app.get('/', routes.index);
//POST signup form.
app.post('/signup', function(req, res) {
var nameField = req.body.name,
emailField = req.body.email,
previewBool = req.body.previewAccess;
res.send(200);
signup(nameField, emailField, previewBool);
});
//Add signup form data to database.
var signup = function (nameSubmitted, emailSubmitted, previewPreference) {
var formData = {
TableName: config.STARTUP_SIGNUP_TABLE,
Item: {
email: {'S': emailSubmitted},
name: {'S': nameSubmitted},
preview: {'S': previewPreference}
}
};
db.putItem(formData, function(err, data) {
if (err) {
console.log('Error adding item to database: ', err);
} else {
console.log('Form data added to database.');
var snsMessage = 'New signup: %EMAIL%'; //Send SNS notification containing email from form.
snsMessage = snsMessage.replace('%EMAIL%', formData.Item.email['S']);
sns.publish({ TopicArn: config.NEW_SIGNUP_TOPIC, Message: snsMessage }, function(err, data) {
if (err) {
console.log('Error publishing SNS message: ' + err);
} else {
console.log('SNS message sent.');
}
});
}
});
};
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});