-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·207 lines (183 loc) · 7.43 KB
/
server.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Define some global constants. The client defines the opposite values
global.__CLIENT__ = false;
global.__SERVER__ = true;
import express from 'express';
import React from 'react';
import { renderToString } from 'react-dom/server';
import { match } from 'react-router';
import { ReduxAsyncConnect, loadOnServer } from 'redux-connect';
import createHistory from 'react-router/lib/createMemoryHistory';
import { syncHistoryWithStore } from 'react-router-redux';
import { Provider } from 'react-redux';
import Helmet from 'react-helmet';
import makeStore from './src/js/redux/store';
import createRoutes from './src/js/routes';
import bodyParser from 'body-parser';
import { SMTP } from 'lock';
export default function server(parameters) {
const SMTPuser = process.env.SMTPUSER || SMTP.user;
const SMTPpass = process.env.SMTPPASS || SMTP.password;
const SMTPhost = process.env.SMTPHOST || SMTP.host;
const SMTPssl = process.env.SMTPSSL || SMTP.ssl;
const app = express();
app.set('view engine', 'ejs');
if (__PROD__) {
app.use('/assets', express.static('./build/assets'));
}
// Adds support for JSON-encoded bodies used in POST requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Processes the form submission
app.post('/send', function (req, res) {
const email = require('emailjs/email');
const server = email.server.connect({
user: SMTPuser,
password: SMTPpass,
host: SMTPhost,
ssl: true,
tls: true,
port: 465,
timeout: 30000
});
console.log(server);
// Build you html for email
const message = '<html><body>' +
'<table width="700" border="0" cellspacing="0" cellpadding="0">' +
'<tr><td> </td><td> </td></tr>' +
'<tr>' +
'<td width="250"><strong>First Name</strong></td>' +
'<td width="450">' + req.body.fname + '</td>' +
'</tr>' +
'<tr><td> </td><td> </td></tr>' +
'<tr>' +
'<td width="250"><strong>Last Name</strong></td>' +
'<td width="450">' + req.body.lname + '</td>' +
'</tr>' +
'<tr><td> </td><td> </td></tr>' +
'<tr>' +
'<td width="250"><strong>Email</strong></td>' +
'<td width="450">' + req.body.email + '</td>' +
'</tr>' +
'<tr><td> </td><td> </td></tr>' +
'<tr>' +
'<td width="250"><strong>Phone Number</strong></td>' +
'<td width="450">' + req.body.phone + '</td>' +
'</tr>' +
'<tr><td> </td><td> </td></tr>' +
'<tr>' +
'<td width="250"><strong>Message</strong></td>' +
'<td width="450">' + req.body.message + '</td>' +
'</tr>' +
'<tr>' +
'<td> </td>' +
'</tr>' +
'</table>' +
'</html></body>';
// If required (string) is passed to POST
// Loop over values, if value.length < 1 then sendEmail = false
let sendEmail = true;
if (req.body.required) {
const requiredFields = req.body.required.split(',');
for (var i = 0; i < requiredFields.length; i++) {
const value = req.body[requiredFields[i]].trim();
if (value.length < 1) {
sendEmail = false;
break;
}
}
}
if (sendEmail) {
server.send({
'text': req.body,
'from': req.body.email,
'to': '[email protected]',
'reply-to': req.body.email,
'subject': `Contact Form Submission (${req.headers.host})`,
attachment: [{data: message, alternative: true}]
}, function(error, message) {
if (error) {
console.log(error);
return res.send({status: 'KO'});
} else {
console.log(message);
return res.send({status: 'OK'});
}
});
} else {
return res.send({status: 'ERROR'});
}
});
/*
This will be the most visited route of our application: it responds to all paths.
For each request that comes to our web server, we will create a new store.
Then, using the match function of react-router, we will receive the tree of components
to render for the current request URL. <Redirect> routes will result in HTTP 302 responses.
Regular routes will result in a call to the loadOnServer function from redux-connect. This
call will return a Promise that is resolved when all the Promises specified in all the
wrapped components are resolved. For an example of this, see how the <Home> component loads its data.
*/
app.get('/*', (req, res) => {
const memHistory = createHistory(req.originalUrl);
const store = makeStore(memHistory);
const history = syncHistoryWithStore(memHistory, store);
const routes = createRoutes(store);
match({ history, routes, location: req.originalUrl }, (err, redirectLocation, renderProps) => {
if (redirectLocation) {
res.redirect(redirectLocation.pathname + redirectLocation.search);
} else if (err) {
console.error('ROUTER ERROR:', error);
res.status(500);
} else if (renderProps) {
loadOnServer({...renderProps, store})
.then(() => {
// Check if there's a 404 after loading data on server
if (store.getState().ssr.error404) {
res.status(404);
}
var html;
try {
html = renderToString(
<Provider store={store} key="provider">
<ReduxAsyncConnect {...renderProps} />
</Provider>
);
}
catch(e) {
html = '';
}
const head = Helmet.rewind();
const title = head.title.toString();
const meta = head.meta.toString();
const link = head.link.toString();
const chunks = parameters.chunks();
const appJs = chunks && chunks.javascript && chunks.javascript.main;
const appCss = chunks && chunks.styles && chunks.styles.main;
res.render('index', {html, title, meta, link, store, appCss, appJs});
})
.catch(err => {
console.error(err.stack);
res.status(500);
if (__DEV__) {
res.send(err.stack);
}
else {
res.send('Server Error');
}
});
} else {
res.status(404).send('Not Found');
}
});
});
const server = require('http').createServer(app);
const PORT = process.env.PORT || 3000;
const IP = process.env.IP || '0.0.0.0';
server.listen(PORT, IP, function(err) {
if (err) {
console.log(err.stack);
}
else {
console.log("Server listening on http://%s:%s", server.address().address, server.address().port);
}
});
}