-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (79 loc) · 3.27 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
var express = require('express');
var morgan = require('morgan');
var nodemailer = require('nodemailer');
var bodyParser = require('body-parser');
var request = require('request');
var favicon = require('serve-favicon');
var hogan = require('hogan.js');
var fs = require('fs');
var config = require('config');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(morgan('short'));
app.use(express.static(__dirname + '/src/assets'));
app.use(favicon(__dirname + '/src/assets/images/favicon.ico'));
var smtpTrans = nodemailer.createTransport(config.mailer);
smtpTrans.verify(function(error, succ){
if (error) {
console.log(error);
} else {
console.log('Server is ready to take our messages');
}
})
function loadSPA(req, res) {
var spa_file = fs.readFileSync(__dirname + '/src/index.html', 'utf8');
var spa_template = hogan.compile(spa_file);
res.send(spa_template.render({
google_maps_api_key: config.google_maps_api_key,
current_year: new Date().getFullYear(),
version: process.env.WEBSITE_VERSION,
}));
}
app.get('/', loadSPA);
app.get('/services', loadSPA);
app.get('/services', loadSPA);
app.get('/about', loadSPA);
app.get('/contact', loadSPA);
app.post('/contact/send-mail', (req, res) => {
var form_data = req.body;
request.post('https://www.google.com/recaptcha/api/siteverify', {form: {secret: config.recaptcha_server_key, response: req.body.captcha}}, function(err, httpResponse, body){
body = JSON.parse(body);
if (err || !body.success){
res.json({type: 'alert', message: 'Error occured, invalid captcha'});
}else{
var company_mail = {
from: 'Wasantha Athukorala Website ' + config.support_email,
to: config.our_email,
subject: 'You have got a new message!',
text: 'The following message was sent by ' + form_data.name + '\n' +
'Contact number is ' + (form_data.contact_num || '-') + '\n' +
'Email is ' + form_data.email + '\n' +
'Message is ' + form_data.message
}
smtpTrans.sendMail(company_mail, function(error, response){
if (error){
res.json({type: 'alert', message: 'Error occured, message not sent'});
}else{
res.json({type: 'success', message: 'Message sent! Thank you!'});
}
});
var email_file = fs.readFileSync(__dirname + '/src/thank_you_email.html', 'utf8');
var email_template = hogan.compile(email_file);
var client_mail = {
from: 'Wasantha Athukorala Sole Propreitorship ' + config.support_email,
to: form_data.email,
subject: 'Enquriy at Wasantha Athukorala Sole Propreitorship',
html: email_template.render({name: form_data.name})
}
smtpTrans.sendMail(client_mail, function(error, response){
if (error){
console.log(error);
}
});
}
})
});
app.listen(process.env.PORT, function(){
console.log(config.util.getEnv('NODE_ENV') + ' server started at localhost:' + process.env.PORT);
});