-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (78 loc) · 2.11 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
var url = require('url');
var fs = require('fs');
var qs = require('querystring');
var nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
auth: {
pass: 'zqMrHHaabkHWFxtUt7'
}
});
function renderHtml(path, res){
fs.readFile(path, null, function(error, data) {
if (error){
res.writeHead(404);
res.write('Arquivo nao encontrado!');
} else {
res.write(data);
}
res.end();
});
}
function sendEmail(req, res){
if (req.method === 'POST'){
var body = '';
req.on('data', function(chunk){
body += chunk;
});
req.on('end', function() {
var data = JSON.parse(body);
res.writeHead(200);
var mailOptions = {
subject: 'Contato',
text: 'Nome: ' + data.fullName +
' Email: ' + data.email +
' Telefone: ' + data.phone +
' Mensagem: ' + data.message,
html: '<ul><li>Nome: ' + data.fullName +
'</li><li>Email: ' + data.email +
'</li><li>Telefone: ' + data.phone +
'</li><li>Mensagem: ' + data.message + '</li>'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
res.end();
})
} else {
res.writeHead(404);
res.end();
}
}
module.exports = {
handleRequest: function (req, res) {
res.writeHead(200, {'Content-type': 'text/html'});
var path = url.parse(req.url).pathname;
console.log('rota ' + path)
switch (path) {
case '/':
renderHtml('./index.html', res);
break;
case '/post':
sendEmail(req, res);
break;
default:
res.writeHead(404);
res.write('Caminho nao encontrado');
res.end();
}
}
}