forked from shiyu3169/ulem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·90 lines (75 loc) · 2.34 KB
/
Copy pathserver.js
File metadata and controls
executable file
·90 lines (75 loc) · 2.34 KB
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
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
const app = express();
//HTTPS redirect middleware
function ensureSecure(req, res, next) {
//Heroku stores the origin protocol in a header variable. The app itself is isolated within the dyno and all request objects have an HTTP protocol.
if (req.get('X-Forwarded-Proto') == 'https' || req.hostname == 'localhost') {
// Don't do anything if the req is comming from https or localhost
next();
} else if (
req.get('X-Forwarded-Proto') != 'https' &&
req.get('X-Forwarded-Port') != '443'
) {
//Redirect if not HTTP with original request URL
res.redirect('https://' + req.hostname + req.url);
}
}
app.use('/', ensureSecure);
const cookieParser = require('cookie-parser');
const session = require('cookie-session');
const passport = require('passport');
app.use(cookieParser());
if (process.env.SESSION_SECRET) {
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true
})
);
} else {
app.use(
session({
secret: 'test',
resave: true,
saveUninitialized: true
})
);
}
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Point static path to dist -- For building -- REMOVE
app.use(express.static(path.join(__dirname, 'build')));
// CORS
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Credentials', 'true');
next();
});
const port = process.env.PORT || '3100';
app.set('port', port);
// Create HTTP server
const server = http.createServer(app);
require('./server/app')(app);
// For Build: Catch all other routes and return the index file -- BUILDING
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'build', 'index.html'));
});
}
// server.listen(port);
server.listen(port, function() {
console.log('Running on ' + app.get('port'));
});