-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (140 loc) · 4.06 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
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
const express = require("express");
const favicon = require("serve-favicon");
const nodemailer = require("nodemailer");
const bunyan = require("bunyan");
const bodyParser = require("body-parser");
const validator = require("express-validator");
const path = require("path");
const fs = require("fs");
const app = express();
// const mailOptions = require('./mailOptions');
app.engine("html", require("ejs").renderFile);
app.set("/views", express.static(__dirname + "/views/pages"));
app.set("view engine", "html");
app.use(favicon(__dirname + "/favicon.ico"));
app.use("/assets", express.static(__dirname + "/assets"));
app.use(express.static(__dirname + "/assets"));
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(validator());
app.get("/", function(request, response) {
response.render("pages/index");
});
app.get("/resume", function(req, res) {
var filePath = "/views/pages/Seth-Bergman-Resume.pdf";
fs.readFile(__dirname + filePath, function(err, data) {
res.contentType("application/pdf");
res.send(data);
});
});
app.get("/thank-you", function(request, response) {
response.render("pages/thank-you");
});
app.get("/resume/print", function(request, response) {
response.render("print/index");
});
/*
app.get('/send', function (req, res) {
res.send('Got a POST request');
res.redirect('/');
});
*/
// app.post('/send', function(req, res) {
// req.checkBody("user_email", "Enter a valid email address.").isEmail();
// req.checkBody("user_email", "Enter a valid email address.");
// req.checkBody('textarea1').notEmpty()
// req.checkBody('name').notEmpty()
// var errors = req.validationErrors();
// if (errors) {
// res.send(errors);
// return;
// } else {
// // normal processing here
// res.render('pages/thank-you');
// }
// });
// var smtpTransport = nodemailer.createTransport('SMTP', {
// service: 'Gmail',
// auth: {
// // enter your gmail account
// user: 'seth.atxwebs@gmail.com',
// // enter your gmail password
// pass: 'gouuvbgiwtcnnbps',
// },
// })
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
type: "SMTP",
user: "seth.atxwebs@gmail.com",
pass: "gouuvbgiwtcnnbps"
// expires: 12345
},
debug: false // include SMTP traffic in the logs
});
app.post("/", function(req, res) {
req.assert("name", "Name is required").notEmpty(); //Validate name
req.assert("email", "A valid email is required").isEmail(); //Validate email
var errors = req.getValidationResult();
if (!errors) {
//No errors were found. Passed Validation!
res.render("pages/index.html", {
title: "Form Validation Example",
message: "Passed Validation!",
errors: {}
});
} else {
//Display errors to user
res.render("pages/index.html", {
title: "Form Validation Example",
message: "",
errors: errors
});
}
});
app.get("/", function(req, res) {
res.sendfile("pages/index.html");
});
app.get("/send", function(req, res) {
var mailOptions = {
to: req.query.to,
subject: "Contact Form Message",
from: "Contact Form Request" + "<" + req.query.from + ">",
html:
"From: " +
req.query.name +
"<br>" +
"User's email: " +
req.query.user +
"<br>" +
"Message: " +
req.query.text
};
// console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function(err, response) {
if (err) {
// console.log(err);
res.end("error");
} else {
// console.log("Message sent: " + response.message);
res.end("sent");
}
});
});
// app.get('/modal1', function (req, res, next) {
// var mailOptions = {
// to: req.query.to,
// subject: 'Contact Form Message',
// from: "Contact Form Request" + "<" + req.query.from + '>',
// html: "From: " + req.query.name + "<br>" +
// "User's email: " + req.query.user + "<br>" + "Message: " + req.query.text
// }
//
// // res.render('pages/thank-you');
// });
const server = app.listen(process.env.PORT || 5000, function() {
console.log("Server running at http://0.0.0.0:" + server.address().port);
});