-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
76 lines (56 loc) · 2.06 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
require('dotenv').config()
var express = require('express');
var path = require('path');
var fs = require('fs');
var moment = require('moment');
const bodyParser = require('body-parser');
const sslRedirect = require('heroku-ssl-redirect');
var app = express();
app.use(sslRedirect())
var isProduction = process.env.NODE_ENV === 'production';
var port = isProduction ? process.env.PORT : 4000;
app.use(express.static(__dirname + '/build'));
app.use(bodyParser.json());
app.get('*', function (req, res, next) {
// Prevents an HTML response for API calls
if (req.path.indexOf('/api/') != -1) {
return next();
}
fs.readFile(__dirname + '/build/index.html', 'utf8', function (err, text) {
res.send(text);
});
});
var cors = require('cors');
var whitelist = [
'http://localhost:8080',
'http://localhost:3000',
'http://localhost:4000',
'https://setlife.network',
'https://info.setlife.network',
'https://www.setlife.network',
'https://setlife-website.herokuapp.com/'
];
var corsOptions = {
origin: function(origin, callback) {
var originIsWhitelisted = whitelist.indexOf(origin) !== -1;
callback(null, originIsWhitelisted);
},
credentials: true,
methods: ['GET,PUT,POST,DELETE,OPTIONS'],
allowedHeaders: ['Access-Control-Allow-Headers', 'Origin', 'Access-Control-Allow-Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Cache-Control']
};
app.use(cors(corsOptions));
const apiModules = require('./api/modules/');
app.post('/api/send/', apiModules.emailSubscriptions.subscribeNewUser);
app.get('/api/unsubscribe/:id', apiModules.emailSubscriptions.unsubscribeUser);
app.get('/api/fetchNewsletter/:month', (request, response) => {
const file = fs.readFileSync(`./docs/newsletters/${request.params.month}.md`)
response.send(file)
});
app.get('/api/fetchAllNewsletters', (request, response) => {
const file = fs.readdirSync('./docs/newsletters/')
response.send(file)
});
app.listen(port, function () {
console.log('SetLife-ReactWithApi: Server running on port ' + port);
});