Skip to content

Commit 721cf95

Browse files
drauggresvitalyrepin
authored andcommitted
feat: redirect from http to https
1 parent 1a300df commit 721cf95

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/server/services/HttpServer.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class HttpServer implements Service {
1818
private static PUBLIC_DIR = DEFAULT_STATIC_DIR;
1919
private static SERVE_STATIC = true;
2020
private servers: ServerAndPort[] = [];
21-
private app?: Express;
21+
private mainApp?: Express;
2222

2323
protected constructor() {
2424
// nothing here
@@ -58,13 +58,13 @@ export class HttpServer implements Service {
5858
}
5959

6060
public start(): void {
61-
this.app = express();
61+
this.mainApp = express();
6262
if (HttpServer.SERVE_STATIC && HttpServer.PUBLIC_DIR) {
63-
this.app.use(express.static(HttpServer.PUBLIC_DIR));
63+
this.mainApp.use(express.static(HttpServer.PUBLIC_DIR));
6464
}
6565
const config = Config.getInstance();
6666
config.getServers().forEach((serverItem) => {
67-
const { secure, port } = serverItem;
67+
const { secure, port, redirectToSecure } = serverItem;
6868
let proto: string;
6969
let server: http.Server | https.Server;
7070
if (secure) {
@@ -86,12 +86,37 @@ export class HttpServer implements Service {
8686
cert = config.readFile(certPath);
8787
}
8888
const options = { ...serverItem.options, cert, key };
89-
server = https.createServer(options, this.app);
89+
server = https.createServer(options, this.mainApp);
9090
proto = 'https';
9191
} else {
9292
const options = serverItem.options ? { ...serverItem.options } : {};
93-
server = http.createServer(options, this.app);
9493
proto = 'http';
94+
let currentApp = this.mainApp;
95+
let host = '';
96+
let port = 443;
97+
let doRedirect = false;
98+
if (redirectToSecure === true) {
99+
doRedirect = true;
100+
} else if (typeof redirectToSecure === 'object') {
101+
doRedirect = true;
102+
if (typeof redirectToSecure.port === 'number') {
103+
port = redirectToSecure.port;
104+
}
105+
if (typeof redirectToSecure.host === 'string') {
106+
host = redirectToSecure.host;
107+
}
108+
}
109+
if (doRedirect) {
110+
currentApp = express();
111+
currentApp.use(function (req, res) {
112+
const url = new URL(`https://${host ? host : req.headers.host}${req.url}`);
113+
if (port && port !== 443) {
114+
url.port = port.toString();
115+
}
116+
return res.redirect(301, url.toString());
117+
});
118+
}
119+
server = http.createServer(options, currentApp);
95120
}
96121
this.servers.push({ server, port });
97122
server.listen(port, () => {

src/types/Configuration.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export interface ServerItem {
1717
secure: boolean;
1818
port: number;
1919
options?: ExtendedServerOption;
20+
redirectToSecure?:
21+
| {
22+
port?: number;
23+
host?: string;
24+
}
25+
| boolean;
2026
}
2127

2228
export interface Configuration {

0 commit comments

Comments
 (0)