-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver-docker.js
98 lines (79 loc) · 2.4 KB
/
server-docker.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
// 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 http from 'http';
import https from 'https';
import { handler } from './build/handler.js';
// 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
import fs from 'fs';
const options = {
key: fs.readFileSync('./certs/privkey.pem'),
cert: fs.readFileSync('./certs/cert.pem'),
}
;
const app = express();
/**
* [ℹ] separate from SvelteKit endpoint in attempts to
* [ℹ] identify clients (IP - address)
*/
/**
app.get('/getClientIP', (req, res) => {
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
* [ℹ] https://jaketrent.com/post/https-redirect-node-heroku
* [ℹ] https://webdva.github.io/how-to-force-express-https-tutorial
*/
// app.use((req, res, next) => {
// if (req.header('x-forwarded-proto') !== 'https')
// res.redirect(`https://${req.header('host')}${req.url}`);
// else
// next();
// });
/**
* [ℹ] https://www.npmjs.com/package/express-sslify
* [ℹ] 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);
// ~~~~~~~~~~~~~~~~~~~~~~
// APP SPIN-UP [PROD] [V2]
// ~~~~~~~~~~~~~~~~~~~~~~
http.createServer(app).listen(80, () => {
console.log(`[HTTP | Server]: Server is running on port: ${80}`);
});
https.createServer(options, app).listen(443, () => {
console.log(`[HTTPS | Server]: Server is running on port: ${443}`);
});