Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
389 lines (267 loc) · 9.1 KB
/
Copy pathserver.js
File metadata and controls
389 lines (267 loc) · 9.1 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
var nodemailer = require('nodemailer');
var express = require('express');
var bodyParser = require('body-parser');
var transporterConfig = require('production.js');
/* to deploy on now.sh, replace:
* var transporterConfig = require('production.js'); //on server.js
* with var transporterConfig along with module.exports = transporterConfig; //on production.js
*/
/* CONSTANTS DECLARATION */
/* CONSTANT WITH TESTERS MAIL - SEPARATE MULTIPLE WITH COMMA */
const TESTER_EMAIL = 'cristiano@elbit.com.br';
//const TESTER_EMAIL = 'cristiano@elbit.com.br, another@mail.com, example@example.com';
/* END - CONSTANTS DECLARATION */
var app = express();
var port = process.env.PORT || 8080;
//handle post params as express does not include it native anymore
//body-parser act as a middleware to parse post requests
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//middleware to set headers for all requests as json and cors related
app.use(function (req, res, next) {
res.header("Content-Type", 'application/json'); //set all routes to content-type application/json
res.header("Access-Control-Allow-Origin", "*"); //enable cors
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
//res.header('Access-Control-Allow-Credentials', true); //for cache control
//OPTIONS handle?
next();
});
app.get('/', function (req, res) {
res.send('');
res.redirect(302, '~site da elbit~'); //(default, 302) found, but redirect to this one instead
});
/*===== GENERIC CODE - ALL SERVICES USES THEN - CHANGE ONLY ON EMERGENCY =====*/
var config = new transporterConfig(); //production variables, for more info see production.js
/* GENERAL TRANSPORTER VARIABLE - SERVER CONFIGURATION */
var transporter = nodemailer.createTransport({
host: config.getHost(),
port: config.getPort(),
auth: {
user: config.getUser(),
pass: config.getPass()
}
});
/**
* Send email to receiver
* @param res current route response
* @param mailOptions where/who to send to
* @param htmlBody what there is to send
*/
function sendEmail(res, mailOptions, htmlBody) {
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.status(500).send({'error': error});
} else {
console.log('Message sent: ' + info.response);
res.send({'success': info.response, 'body': htmlBody});
}
});
}
/**
* Receive request type as param and return generic json body
* returns: type => request type ; params => list of "supported" fields on form all forms ; literal => fields names
* @param {string} request_type as get or post (only for info display)
* @returns {{type: *, params: {cName: string, cPhone: string, cEmail: string, cAddress: string, cCity: string, cMessage: string}, literal: {cName: string, cPhone: string, cEmail: string, cAddress: string, cCity: string, cMessage: string}}}
*/
function genericJsonBody(request_type) {
return {
type: request_type,
params: {
cName: '',
cPhone: '',
cEmail: '',
cAddress: '',
cCity: '',
cMessage: ''
},
literal: {
cName: 'Nome',
cPhone: 'Telefone',
cEmail: 'Email',
cAddress: 'Endereço',
cCity: 'Cidade',
cMessage: 'Mensagem'
}
};
}
/*===== END - GENERIC CODE =====*/
/* <!-- */
/* DEVELOPMENT ROUTES */
app.get('/form', function (req, res) {
res.header('Content-Type', 'text/html');
res.sendfile("form.html");
});
app.get('/venus/get', function (req, res) {
if (Object.keys(req.query).length === 0) {
res.send({response: 'Não há nenhum parâmetro'});
} else {
var htmlBody = "GET:<br><br>";
var jsonBody = {
type: 'get',
params: {}
};
var params = req.query;
for (var query in params) {
//if (params.hasOwnProperty(query)) {
htmlBody += query + ': <b>' + params[query] + '</b><br>';
jsonBody.params[query] = params[query];
//}
}
res.send(jsonBody);
//res.send(htmlBody);
}
});
app.post('/venus/post', function (req, res) {
if (Object.keys(req.body).length === 0) {
res.send({response: 'POST vazio'});
} else {
var htmlBody = "POST:<br><br>";
var jsonBody = {
type: 'post',
params: {}
};
var params = req.body;
for (var query in params) {
//if (params.hasOwnProperty(query)) {
htmlBody += query + ': <b>' + params[query] + '</b><br>';
jsonBody.params[query] = params[query];
//}
}
res.send(jsonBody);
//res.send(htmlBody);
}
});
/* END - DEVELOPMENT ROUTES */
/* --> */
/* <!-- */
/* SERVICE CHECK ROUTES */
app.get('/mailman/check/send', function (req, res) {
if (Object.keys(req.query).length === 0) {
res.send({response: 'Não há nenhum parâmetro'});
} else {
var htmlBody = 'Development request para testar formulário de contato<br><br><b><i>Variáveis recebidas:</i></b><br>';
var params = req.query;
for (var query in params) {
//if (params.hasOwnProperty(query)) {
htmlBody += query + ': <b>' + params[query] + '</b><br>';
//}
}
htmlBody += 'Cache-control count: <b>' + new Date().getTime() + '</b>';
var mailOptions = {
from: '"Mailman" <mailman@service.elbit.com.br>',
to: TESTER_EMAIL,
subject: 'Contato - Formulário',
html: htmlBody
};
sendEmail(res, mailOptions, htmlBody);
}
});
app.get('/mailman/check/scoped', function (req, res) {
if (Object.keys(req.query).length === 0) {
res.send({response: 'Não há nenhum parâmetro'});
} else {
var htmlBody = 'Development request<br><br><b><i>Variáveis recebidas (escopo):</i></b><br>';
var jsonBody = genericJsonBody('get');
var params = req.query;
for (var query in params) {
if (jsonBody.params[query] === '') {
jsonBody.params[query] = params[query];
htmlBody += jsonBody.literal[query] + ": <b>" + jsonBody.params[query] + "</b><br>";
}
}
htmlBody += 'Cache-control count: <b>' + new Date().getTime() + '</b>';
var mailOptions = {
from: '"Mailman" <mailman@service.elbit.com.br>',
to: TESTER_EMAIL,
subject: 'Contato - Formulário',
html: htmlBody
};
sendEmail(res, mailOptions, htmlBody);
}
});
app.get('/mailman/check/valid', function (req, res) {
res.status(200).send({response: 'WORKING'});
});
app.get('/mailman/check/invalid', function (req, res) {
res.status(500).send({error: 'CHECKING INVALID'});
});
/* END - SERVICE CHECK ROUTES */
/* --> */
/* SERVICE ROUTES */
/* CITODON */
/**
* Post route handle, to port it to get route, modify:
* app.get('')
* Object.keys(req.query) //and change error message
* var jsonBody = genericJsonBody('get')
* var params = req.query;
*/
app.post('/mailman/citodon', function (req, res) {
if (Object.keys(req.body).length === 0) {
res.send({response: 'POST vazio (¿)'});
} else {
var htmlBody = "Foi realizado um pedido de contato pelo site!<br/>";
var textBody = "Foi realizado um pedido de contato pelo site! ";
var jsonBody = genericJsonBody('post');
var params = req.body;
for (var query in params) {
//there is no hasOwnProperty on req.body (throws error)
//if (params.hasOwnProperty(query)) {}
//is it on the generic object and is not empty? (as it can be sent from the form page empty)
if (jsonBody.params[query] === '' && params[query] !== '') {
jsonBody.params[query] = params[query];
htmlBody += jsonBody.literal[query] + ": <b>" + jsonBody.params[query] + "</b><br>";
textBody += jsonBody.literal[query] + ": " + jsonBody.params[query] + ", ";
}
}
var mailOptions = {
from: '"Mailman" <mailman@service.elbit.com.br>',
// to: 'contato@citodon.com.br',
to: TESTER_EMAIL,
bcc: 'cristiano@elbit.com.br, joseeduardobarros@gmail.com',
replyTo: jsonBody.params.cEmail, //if not defined, will be empty anyways
subject: 'Requisição de contato - Citodon',
text: textBody,
html: htmlBody
};
//sendEmail(res, mailOptions, htmlBody);
// res.send({'html': htmlBody, 'json': jsonBody});
res.send(jsonBody);
}
});
app.get('/mailman/citodon', function (req, res) {
if (Object.keys(req.query).length === 0) {
res.send({response: 'Nenhum parâmetro especifiado'});
} else {
var htmlBody = "Foi realizado um pedido de contato pelo site!<br/>";
var textBody = "Foi realizado um pedido de contato pelo site! ";
var jsonBody = genericJsonBody('get');
var params = req.query;
for (var query in params) {
if (jsonBody.params[query] === '' && params[query] !== '') {
jsonBody.params[query] = params[query];
htmlBody += jsonBody.literal[query] + ": <b>" + jsonBody.params[query] + "</b><br>";
textBody += jsonBody.literal[query] + ": " + jsonBody.params[query] + ", ";
}
}
var mailOptions = {
from: '"Mailman" <mailman@service.elbit.com.br>',
to: 'contato@citodon.com.br',
// to: TESTER_EMAIL,
bcc: 'cristiano@elbit.com.br, joseeduardobarros@gmail.com',
replyTo: jsonBody.params.cEmail,
subject: 'Requisição de contato - Citodon',
text: textBody,
html: htmlBody
};
sendEmail(res, mailOptions, htmlBody);
// res.send({'html': htmlBody, 'json': jsonBody});
// res.send(jsonBody);
}
});
/* NODE SERVER START */
app.listen(port, function () {
console.log('Servidor iniciado na porta: ' + port);
});