-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (67 loc) · 2.11 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
const express = require('express');
const app = express();
var mg = require('nodemailer-mailgun-transport');
var path = require('path');
var bodyParser = require('body-parser');
var env = require("./env.js");
const router = express.Router();
const nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var cacheLifetime = 60*60*24*365*1000;
//app.use(express.static(__dirname + '/static', { maxAge: cacheLifetime }));
app.use(express.static(path.join(__dirname, 'public')));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({extended: true}));
/**bodyParser.json(options)
* Parses the text as JSON and exposes the resulting object on req.body.
*/
app.use(bodyParser.json());
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.get('/', router);
app.post('/contact', function(req, response){
sendEmail(req.body);
response.json({"done" : true, "status" : 200});
});
app.post('/vendor', function(req, response){
if (req.body.pass == env.password()) {
console.log('ok password');
response.render(__dirname + "/public/vendor.html");
} else {
console.log('failed password')
response.json({"done" : true, "msg": "Unauthorized", "status" : 401});
}
});
function sendEmail(data) {
var auth = {
auth: {
api_key: env.mailgunPearlsAPI(),
domain: env.mailgunPearlsDomain()
}
};
var nodemailerMailgun = nodemailer.createTransport(mg(auth));
nodemailerMailgun.sendMail(
{
from: data.email, // sender address
to: env.himo(), // list of receivers
subject: "from " + data.fullName + ":" + data.subject, // Subject line
text: data.message // plaintext body
},
function (err, info) {
if (err) {
console.log('Error: ' + err);
} else {
console.log('Response: ' + info);
}
});
};
app.use("*",function(req,res){
//res.sendFile(__dirname + "/public/404.html");
});
const port = 3000;
app.listen(process.env.PORT || 3000, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is now listening on ${port}`)
});