-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver-heroku.js
93 lines (77 loc) · 2.33 KB
/
server-heroku.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
// DOC: https://github.com/sveltejs/kit/tree/master/packages/adapter-node#custom-server
// DOC: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
import express from 'express';
import { handler } from './build/handler.js';
// import sslRedirect from 'heroku-ssl-redirect';
// DOC: https://expressjs.com/en/resources/middleware/compression.html
import compression from 'compression';
import * as sslify from 'express-sslify';
// DOC: https://www.npmjs.com/package/request-ip
const app = express();
/**
* [ℹ] enable ssl redirect
* DOC: https://www.npmjs.com/package/heroku-ssl-redirect
* => does not seem to be working
*/
// app.use(sslRedirect());
/**
* [ℹ] separate from SvelteKit endpoint in attempts to
* [ℹ] identify clients (IP - address)
*/
/**
app.get('/getClientIP', (req, res, next) => {
const ip = req.headers['x-forwarded-for'] ||
req.socket.remoteAddress ||
null;
console.log('ip', ip);
const ip2 = req.ip
console.log('ip2', ip2);
const ip3 = requestIp.getClientIp(req);
console.log('ip3', ip3);
let ipAddr = req.headers["x-forwarded-for"];
if (ipAddr){
const list = ipAddr.split(",");
ipAddr = list[list.length-1];
} else {
ipAddr = req.connection.remoteAddress;
}
console.log('ipAddr', ipAddr);
res.json(
{
"ip": ip.toString().replace(/,/g, '')
}
);
res.end()
})
*/
/**
* [ℹ] [FORCE] https-redirect
* DOC: https://jaketrent.com/post/https-redirect-node-heroku
* DOC: https://webdva.github.io/how-to-force-express-https-tutorial
* [HEROKU]
*/
app.use((req, res, next) => {
if (req.header('x-forwarded-proto') !== 'https')
res.redirect(`https://${req.header('host')}${req.url}`);
else
next();
});
/**
* DOC: https://www.npmjs.com/package/express-sslify
* DOC: https://stackoverflow.com/questions/51234023/heroku-nodejs-redirect-http-to-http
*/
app.use(sslify.HTTPS({ trustProtoHeader: true }))
// [ℹ] compress all responses
app.use(compression())
/**
* [ℹ] let SvelteKit handle everything else,
* [ℹ] including serving prerendered pages and static assets
*/
app.use(handler);
/**
* [ℹ] initialize app;
* DOC: https://stackoverflow.com/questions/15693192/heroku-node-js-error-web-process-failed-to-bind-to-port-within-60-seconds-o
*/
app.listen(process.env.PORT || 5000, () => {
console.log('listening on port 5000');
});