This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
app.js
133 lines (114 loc) · 4.37 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
// Module Dependencies
// -------------------
var express = require('express');
var http = require('http');
var JWT = require('./lib/jwtDecoder');
var path = require('path');
var request = require('request');
var routes = require('./routes');
var activity = require('./routes/activity');
var trigger = require('./routes/trigger');
var app = express();
// Register configs for the environments where the app functions
// , these can be stored in a separate file using a module like config
var APIKeys = {
appId : '__insert_your_app_id__',
clientId : '__insert_your_app_client_id__',
clientSecret : '__insert_your_app_client_secret__',
appSignature : '__insert_your_app_signature__',
authUrl : 'https://auth.exacttargetapis.com/v1/requestToken?legacy=1'
};
// Simple custom middleware
function tokenFromJWT( req, res, next ) {
// Setup the signature for decoding the JWT
var jwt = new JWT({appSignature: APIKeys.appSignature});
// Object representing the data in the JWT
var jwtData = jwt.decode( req );
// Bolt the data we need to make this call onto the session.
// Since the UI for this app is only used as a management console,
// we can get away with this. Otherwise, you should use a
// persistent storage system and manage tokens properly with
// node-fuel
req.session.token = jwtData.token;
next();
}
// Use the cookie-based session middleware
app.use(express.cookieParser());
// TODO: MaxAge for cookie based on token exp?
app.use(express.cookieSession({secret: "HelloWorld-CookieSecret"}));
// Configure Express
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.favicon());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// Express in Development Mode
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// HubExchange Routes
app.get('/', routes.index );
app.post('/login', tokenFromJWT, routes.login );
app.post('/logout', routes.logout );
// Custom Hello World Activity Routes
app.post('/ixn/activities/hello-world/save/', activity.save );
app.post('/ixn/activities/hello-world/validate/', activity.validate );
app.post('/ixn/activities/hello-world/publish/', activity.publish );
app.post('/ixn/activities/hello-world/execute/', activity.execute );
// Custom Hello World Trigger Route
app.post('/ixn/triggers/hello-world/', trigger.edit );
// Abstract Event Handler
app.post('/fireEvent/:type', function( req, res ) {
var data = req.body;
var triggerIdFromAppExtensionInAppCenter = '__insert_your_trigger_key_here__';
var JB_EVENT_API = 'https://www.exacttargetapis.com/interaction-experimental/v1/events';
var reqOpts = {};
if( 'helloWorld' !== req.params.type ) {
res.send( 400, 'Unknown route param: "' + req.params.type +'"' );
} else {
// Hydrate the request
reqOpts = {
url: JB_EVENT_API,
method: 'POST',
headers: {
'Authorization': 'Bearer ' + req.session.token
},
body: JSON.stringify({
ContactKey: data.alternativeEmail,
EventDefinitionKey: triggerIdFromAppExtensionInAppCenter,
Data: data
})
};
request( reqOpts, function( error, response, body ) {
if( error ) {
console.error( 'ERROR: ', error );
res.send( response, 400, error );
} else {
res.send( body, 200, response);
}
}.bind( this ) );
}
});
app.get('/clearList', function( req, res ) {
// The client makes this request to get the data
activity.logExecuteData = [];
res.send( 200 );
});
// Used to populate events which have reached the activity in the interaction we created
app.get('/getActivityData', function( req, res ) {
// The client makes this request to get the data
if( !activity.logExecuteData.length ) {
res.send( 200, {data: null} );
} else {
res.send( 200, {data: activity.logExecuteData} );
}
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});