-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
168 lines (123 loc) · 4.32 KB
/
server.js
File metadata and controls
168 lines (123 loc) · 4.32 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// Access-Control-Header-Origin middleware
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Pass to next layer of middleware
next();
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'API is running!' });
});
// form submit
// ----------------------------------------------------
router.route('/submit')
.post(function(req, res) {
//require Node modules
var https = require('https');
var querystring = require('querystring');
// build the data object
var postData = querystring.stringify({
'email': req.body.email,
'firstname': req.body.firstname,
'lastname': req.body.lastname,
'hs_context': JSON.stringify({
"hutk": req.cookies.hubspotutk,
"ipAddress": req.headers['x-forwarded-for'] || req.connection.remoteAddress,
"pageUrl": "https://www.phrasingpro.com/api",
"pageName": "Forms API - Tech Test"
})
});
// set the post options, changing out the HUB ID and FORM GUID variables.
var options = {
hostname: 'forms.hubspot.com',
path: '/uploads/form/v2/1966653/994ca53c-309a-435d-8b5a-205f668f3173',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
}
// set up the request
var request = https.request(options, function(response){
console.log("Status: " + response.statusCode);
console.log("Headers: " + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function(chunk){
console.log('Body: ' + chunk)
});
});
request.on('error', function(e){
console.log("Problem with request " + e.message)
});
// post the data
request.write(postData);
request.end();
res.json({message: res.statusCode});
});
// trigger
// ----------------------------------------------------
router.route('/trigger')
.get(function(req, res) {
//require Node modules
var https = require('https');
var querystring = require('querystring');
// build the data object
var getData = querystring.stringify({
'email': req.body.email
});
// set the post options, changing out the HUB ID and FORM GUID variables.
var options = {
hostname: 'track.hubspot.com',
path: '/v1/event?_n=clickedButton&_a=1966653&email=' + req.body.email,
method: 'GET',
headers: {
"User-Agent": "Mozilla/5.0"
}
}
// set up the request
var request = https.request(options, function(response){
console.log("Status: " + response.statusCode);
console.log("Headers: " + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function(chunk){
console.log('Body: ' + chunk)
});
});
request.on('error', function(e){
console.log("Problem with request " + e.message)
});
// post the data
request.write(getData);
request.end();
res.json({message: res.statusCode});
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Server is running on port ' + port);