-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.js
137 lines (121 loc) · 3.26 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const multer = require('multer');
const upload = multer();
const app = express();
const config = require('./config')[process.env.NODE_ENV || 'dev'];
const utils = require('./utils');
// setting view engine as ejs with file extension .html
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(
bodyParser.json({
limit: '8mb'
})
); // support json encoded bodies
app.use(
bodyParser.urlencoded({
limit: '8mb',
extended: true
})
); // support encoded bodies
// for parsing multipart/form-data
app.use(upload.array());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res) {
res.render('flowers');
});
app.get('/send', function(req, res) {
const params = req.query;
const receiverEmail = params.email;
if (receiverEmail) {
utils.sendPromotionalEmail(receiverEmail)
.then(data => {
res.json({
success: true,
response: {
envelope: data.envelope,
messageId: data.messageId
}
});
})
.catch(error => {
res.json({
success: false,
message: error.message
});
});
} else {
res.json({
success: false,
message: 'No recipient found'
});
}
});
// setting response headers required by AMP for all our flowers API
app.use('/flowers', function(req, res, next) {
// in production you can extract token from query params and validate it
let origin = 'https://mail.google.com';
let sourceOrigin = config.gmailAccount.user;
// you can also validate that request came from Gmail using proxy assertion token
//const proxyAssertionToken = req.get('Amp4Email-Proxy-Assertion');
if (req.headers['amp-same-origin'] === 'true') {
origin = req.query.__amp_source_origin;
sourceOrigin = origin;
}
res.set({
'Access-Control-Allow-Origin': origin,
'AMP-Access-Control-Allow-Source-Origin': sourceOrigin,
'Access-Control-Allow-Source-Origin':
'AMP-Access-Control-Allow-Source-Origin',
'Access-Control-Expose-Headers':
'Access-Control-Allow-Origin' +
', AMP-Access-Control-Allow-Source-Origin' +
', Access-Control-Allow-Source-Origin'
});
next();
});
// this API will be used in amp email to fetch list of flowers
app.get('/flowers', function(req, res) {
res.json({
success: true,
data: [
{
name: 'Lily',
stars: 5,
price: 70,
url: config.url + 'images/lily.jpeg'
},
{
name: 'Rose',
stars: 3,
price: 50,
url: config.url + 'images/rose.jpeg'
},
{
name: 'Marigold',
stars: 4,
price: 80,
url: config.url + 'images/marigold.jpeg'
},
{
name: 'Jasmine',
stars: 5,
price: 100,
url: config.url + 'images/jasmine.jpeg'
}
]
});
});
// this API will be used in amp email to submit feedback for a flower type
app.post('/flowers/feedback', function(req, res) {
const feedback = req.body;
res.json({
success: true,
message: 'Thank you for your feedback.',
feedback
});
});
module.exports = app;