-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (68 loc) · 2.26 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
const next = require('next');
const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const axios = require('axios');
const { prisma } = require('./utils/prisma/init.js');
const ports = {
http: 3080,
https: 3443,
};
// eslint-disable-next-line no-undef
if (process.env.GITPOD_WORKSPACE_URL) {
const urlWithPort = process.env.GITPOD_WORKSPACE_URL.replace('https://', 'https://' + ports.https + '-');
process.env.URL = urlWithPort;
process.env.NEXTAUTH_URL = urlWithPort;
}
const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = 3443;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
const server = express();
const options = {
key: fs.readFileSync('localhost.key'),
cert: fs.readFileSync('localhost.crt'),
};
console.log('in server.js');
app.prepare().then(() => {
console.log('app prepared');
const handleSubdomainRedirects = async (req, res, next) => {
const host = req.headers.host;
//console.log('host', host);
const subdomain = host.split('.')[0]; // Extract subdomain
//console.log('subdomain', subdomain);
if (subdomain && subdomain != 'www') {
// Redirect to another page or route
let subdomainRedirect;
try {
subdomainRedirect = await prisma.subdomainRedirect.findFirst({ where: { subdomain } });
//console.log('subdomainredirect', subdomainRedirect)
// eslint-disable-next-line no-empty
} catch (err) {}
if (subdomainRedirect?.redirect) res.redirect(subdomainRedirect.redirect);
else next();
} else {
next();
}
};
server.use(handleSubdomainRedirects);
server.all('*', (req, res) => {
return handle(req, res);
});
http.createServer(server).listen(ports.http);
https.createServer(options, server).listen(ports.https);
console.log('Running https at', 'https://localhost:3443');
axios.get('https://localhost:3443/api/init-data');
});
var forceSsl = function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
return next();
};
if (!dev) {
app.use(forceSsl);
}