forked from googlecodelabs/amp-e-commerce
/
app.js
321 lines (287 loc) · 7.21 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Copyright 2018 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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.
*/
var express = require('express');
var formidable = require('formidable');
var cookieParser = require('cookie-parser');
var app = express();
app.use(cookieParser());
app.set('view engine', 'html');
app.engine('html', require('hogan-express'));
app.locals.delimiters = '<% %>';
var axios = require('axios');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var AUTH_COOKIE_MAX_AGE = 1000 * 60 * 60 * 2; //2 hours
var products = [{
"name": "Jeans"
}, {
"name": "Hoody"
}, {
"name": "Hat"
}];
var countries = {
"items": [{
"countries": [{
name: "US"
}, {
name: "UK"
}]
}]
};
var skuToSizeAndPrice = {
"1001": {
"sizes": {
"XS": 8.99,
"S": 9.99,
},
},
"1002": {
"sizes": {
"S": 10.99,
"M": 12.99,
"L": 14.99,
},
},
"1010": {
"sizes": {
"L": 11.99,
"XL": 13.99,
},
},
"1014": {
"sizes": {
"M": 7.99,
"L": 9.99,
"XL": 11.99,
},
},
"1015": {
"sizes": {
"XS": 8.99,
"S": 10.99,
"L": 15.99,
},
},
"1016": {
"sizes": {
"S": 8.99,
"L": 14.99,
"XL": 11.99,
},
},
"1021": {
"sizes": {
"XS": 8.99,
"S": 9.99,
"M": 12.99,
},
},
"1030": {
"sizes": {
"M": 10.99,
"L": 11.99,
},
},
};
app.get('/shirts/sizesAndPrices', function(req, res) {
var sku = req.query.sku;
var response = {};
response[sku] = skuToSizeAndPrice[sku];
setTimeout(() => res.json(response), 1000); // Simulate network delay.
});
app.get('/countries.json', function(req, res) {
assertCors(req, res);
res.send(countries);
});
app.get('/shipping', function(req, res) {
assertCors(req, res);
var nextDayAvailability = {};
var location = req.query.location;
if (location && location == 'UK') {
nextDayAvailability = {
"next-day-availability": "not available"
}
} else {
nextDayAvailability = {
"next-day-availability": "available"
}
}
res.send(nextDayAvailability)
});
app.get('/autosuggest/search_list', function(req, res) {
var query = req.query.q;
var response = {};
var new_obj_array = products.filter(function(obj) {
if (obj.name.toLowerCase().includes(query.toLowerCase())) {
return true;
}
return false;
});
response = new_obj_array;
assertCors(req, res);
res.json(response);
});
app.get('/login', function(req, res) {
assertCors(req, res);
var returnUrl = req.query.return;
res.render('login.html', {
returnUrl: req.query.return
});
});
app.post('/submit', function(req, res) {
var email = req.body.email;
var password = req.body.password;
var returnUrl = req.body.returnurl;
assertCors(req, res);
// set user as logged in via cookie
res.cookie('ampEmailLogin', email, {
maxAge: AUTH_COOKIE_MAX_AGE // 2hr
});
res.redirect(returnUrl + '#success=true');
});
app.get('/logout', function(req, res) {
var email = req.cookies.ampEmailLogin;
if (email) {
res.clearCookie('ampEmailLogin');
}
assertCors(req, res);
res.redirect(req.header('Referer') || '/');
});
app.get('/authorization', function(req, res) {
var response = {};
var deals = {};
if (req.cookies.ampEmailLogin) {
if (req.cookies.ampEmailLogin == 'test1@example.com') {
deals = [{
link: "/deals/tshirt-test1.jpg",
img: "/deals/tshirt-test1.jpg",
name: "tshirt"
}, {
link: "/deals/hoody-test1.jpg",
img: "/deals/hoody-test1.jpg",
name: "hoody"
}]
} else {
deals = [{
link: "/deals/hoody-test2.jpg",
img: "/deals/hoody-test2.jpg",
name: "hoody"
}]
}
response = {
'return': true,
'loggedIn': true,
'deals': deals,
'name': req.cookies.ampEmailLogin.replace("@example.com", "")
};
} else {
response = {
'return': true,
'loggedIn': false
};
}
assertCors(req, res);
res.json(response);
});
app.get("/location-specific-results.json", function(req, res) {
var ip = 'google.com';
axios.get('https://freegeoip.net/json/' + ip).then(response => {
assertCors(req, res);
res.send({
location: response.data.city
});
});
});
app.post('/shirts/addToCart', function(req, res) {
// Necessary for AMP CORS security protocol.
// @see https://github.com/ampproject/amphtml/blob/master/spec/amp-cors-requests.md
assertCors(req, res);
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields) {
if (fields.color && fields.size && fields.quantity) {
res.status(200).json(fields);
} else {
res.status(400).json({
error: 'Please select a size.'
});
}
});
});
app.post('/favorite', function(req, res) {
var id = req.query.id;
assertCors(req, res);
if (req.cookies.ampFavorite && req.cookies.ampFavorite == 'true') {
res.clearCookie('ampFavorite');
res.cookie('ampFavorite', false);
res.json(false)
} else {
res.cookie('ampFavorite', true);
res.json(true)
}
});
app.get('/favorite', function(req, res) {
var id = req.query.id;
assertCors(req, res);
if (req.cookies.name == 'ampFavorite') {
res.json(true)
} else {
res.json(false)
}
});
function assertCors(req, res, opt_validMethods, opt_exposeHeaders) {
const validMethods = opt_validMethods || ['GET', 'POST', 'OPTIONS'];
const invalidMethod = req.method + ' method is not allowed. Use POST.';
const invalidOrigin = 'Origin header is invalid.';
const invalidSourceOrigin = '__amp_source_origin parameter is invalid.';
const unauthorized = 'Unauthorized Request';
var origin;
if (validMethods.indexOf(req.method) == -1) {
res.statusCode = 405;
res.end(JSON.stringify({
message: invalidMethod
}));
throw invalidMethod;
}
if (req.headers.origin) {
origin = req.headers.origin;
} else if (req.headers['amp-same-origin'] == 'true') {
origin = getUrlPrefix(req);
} else {
return;
}
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Expose-Headers', [
'AMP-Access-Control-Allow-Source-Origin'
]
.concat(opt_exposeHeaders || []).join(', '));
if (req.query.__amp_source_origin) {
res.setHeader('AMP-Access-Control-Allow-Source-Origin',
req.query.__amp_source_origin);
} else {
res.setHeader('AMP-Access-Control-Allow-Source-Origin',
origin);
}
}
function getUrlPrefix(req) {
return req.protocol + '://' + req.headers.host;
}
app.use('/', express.static('static'));
app.listen(3000, function() {
console.log(
'Server for "E-commerce AMP" codelab listening on port 3000!'
);
});