-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
71 lines (63 loc) · 1.9 KB
/
index.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
const http = require('http');
const fs = require('fs');
const formidable = require('formidable');
const util = require('util');
const nodemailer = require('nodemailer');
const port = 1337;
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'Sdxk4115'
}
});
//Creating the node.js webserver.
var server = http.createServer((req, res) => {
//Anonymous func to route by request type.
if (req.method.toLowerCase() === 'get') {
present(res);
} else if (req.method.toLowerCase() === 'post') {
processForm(req, res);
}
}).listen(port);
//Presents form.
function present(res) {
fs.readFile('Contact.html', (err, data) => {
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
res.write(data);
res.end();
})
}
//Presents the data from the POST request.
function processForm(req, res) {
var form = new formidable.IncomingForm();
var s = "";
form.parse(req, (err, fields, files) => {
console.log(fields);
});
console.log(s);
let mailOptions = {
from: '"Kieran McBride" <[email protected]>',
to: '[email protected]',
subject: 'Contact Reqest',
text: 'Thanks for contacting us. This is an automated email and an operator will be in touch within 3-5 working days.',
}
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent %s!', info.messageId, info.response);
});
fs.readFile('Contact.html', (err, data) => {
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': data.length
});
res.write(data);
res.end();
});
};
console.log("Listening on : " + port);