-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (54 loc) · 1.87 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
var express = require('express'),
path = require('path'),
httpProxy = require('http-proxy'),
bodyParser = require('body-parser'),
publicPath = path.resolve(__dirname, 'public'),
isProduction = process.env.NODE_ENV === 'production',
fetch = require('isomorphic-fetch');
if (!isProduction) {
var config = require('./config.js');
}
var port = process.env.PORT || 3000;
// We need to add a configuration to our proxy server,
// as we are now proxying outside localhost
var proxy = httpProxy.createProxyServer({
changeOrigin: true
});
var app = express();
//serving our index.html
app.use(express.static(publicPath));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
//server/compiler.js runs webpack-dev-server which creates the bundle.js which index.html serves
//the compiler adds some console logs for some extra sugar
//notice that you will not see a physical bundle.js because webpack-dev-server runs it from memory
var bundle = require('./server/compiler.js');
bundle();
//express now processes all requests to localhost:8080
//app.all is a special routing method used for loading middleware functions
app.all('/build/*', function (req, res) {
proxy.web(req, res, {
target: 'http://localhost:8080'
});
});
proxy.on('error', function(e) {
console.log('Could not connect to proxy, please try again...');
});
app.route('/api/email')
.post(function(req, res){
fetch("https://api.sendgrid.com/v3/mail/send", {
method: "POST",
headers: {
"authorization": process.env.sendgridAuth || config.sendgridAuth,
"Content-Type": "application/json"
},
body: JSON.stringify(req.body)
}).then((response, req) => {
res.status(response.status).send(response);
});
});
app.listen(port, function () {
console.log('Server running on port ' + port);
});