-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
77 lines (66 loc) · 2.17 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
var express = require('express');
var nopt = require('nopt');
var path = require('path');
var bodyParser = require('body-parser');
var earlyaccess = require("./earlyaccess");
var cookieSession = require('cookie-session');
var config = require('./conf.json');
var querystring = require('querystring');
var log4js = require('log4js');
var logger = log4js.getLogger();
log4js.replaceConsole();
var staticPath = config.staticPath;
var port = config.port;
var app = express();
app.use(cookieSession({
name: 'rt_session',
secret: config.session.key,
signed: true
}));
// User tracking middleware
// look for "source" query string which means this is an entry page.
// we then track the source, adwords, twitter, etc.. and all of the other
// params passed.
app.use(function(req, res, next) {
if (req.query.source) {
Object.keys(req.query).forEach(function(key) {
req.session[key] = req.query[key];
});
}
next();
});
app.use(express.static(staticPath));
var urlencodedParser = bodyParser.urlencoded({ extended: false })
//handle earlyaccess request.
app.post('/earlyaccess', urlencodedParser, function (req, res) {
var email = req.body.EMAIL;
var source = req.body.SOURCE;
var newsletter = req.body.NEWSLETTER;
var autopilotSessionId = req.body._autopilot_session_id;
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var ua = req.headers['user-agent'];
if (!email || !source) {
return res.status(400).send("invalid form data.");
}
//queue up the request for asynchronous processing.
earlyaccess.enqueue(email, source, newsletter, autopilotSessionId, req.session, ua, ip).then(function() {
var queryParams = {
email: email
};
Object.keys(req.session).forEach(function(key) {
if (key === "_ctx") {
return;
}
queryParams["tracking_"+key] = req.session[key];
});
var queryStr = querystring.stringify(queryParams);
res.redirect("/earlyaccess?"+queryStr);
}, function(err) {
return res.status(500).send(err);
});
});
var server = app.listen(port, 'localhost', function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});