11import express from 'express' ;
2+ import https from 'https' ;
23import cors from 'cors' ;
34import { fileURLToPath } from 'url' ;
45import { dirname , join } from 'path' ;
@@ -40,6 +41,8 @@ function safeJsonParse(jsonString, defaultValue = {}) {
4041
4142const app = express ( ) ;
4243const PORT = process . env . PORT || 3001 ;
44+ const ENABLE_HTTPS = String ( process . env . ENABLE_HTTPS || '' ) . toLowerCase ( ) === 'true' || process . env . ENABLE_HTTPS === '1' ;
45+ const SSL_PORT = Number . parseInt ( process . env . SSL_PORT || '' , 10 ) || 8443 ;
4346const FRONTEND_URL = process . env . FRONTEND_URL || `http://localhost:${ PORT } ` ;
4447const COOKIE_SECRET = process . env . COOKIE_SECRET || 'dev-cookie-secret' ;
4548
@@ -1162,8 +1165,8 @@ app.get('*', spaLimiter, (req, res) => {
11621165 res . sendFile ( join ( __dirname , '../dist/index.html' ) ) ;
11631166} ) ;
11641167
1165- app . listen ( PORT , '0.0.0.0' , ( ) => {
1166- console . log ( `Server running on port ${ PORT } ` ) ;
1168+ app . listen ( PORT , '0.0.0.0' , async ( ) => {
1169+ console . log ( `HTTP server running on port ${ PORT } ` ) ;
11671170 console . log ( `Frontend: http://localhost:${ PORT } ` ) ;
11681171 console . log ( `API: http://localhost:${ PORT } /api` ) ;
11691172 console . log ( 'Rate limiting active:' ) ;
@@ -1172,4 +1175,31 @@ app.listen(PORT, '0.0.0.0', () => {
11721175 console . log ( '- Login attempts: 5 failed/10min per IP' ) ;
11731176 console . log ( '- Force reset: 2 requests/hour per IP' ) ;
11741177 console . log ( 'Trust proxy:' , app . get ( 'trust proxy' ) ? 'Enabled' : 'Disabled' ) ;
1178+
1179+ if ( ENABLE_HTTPS ) {
1180+ try {
1181+ const mod = await import ( 'selfsigned' ) ;
1182+ const selfsigned = mod . default || mod ;
1183+ const attrs = [ { name : 'commonName' , value : 'localhost' } ] ;
1184+ const pems = selfsigned . generate ( attrs , {
1185+ days : 7 ,
1186+ keySize : 2048 ,
1187+ algorithm : 'sha256'
1188+ } ) ;
1189+
1190+ const httpsServer = https . createServer ( { key : pems . private , cert : pems . cert } , app ) ;
1191+ httpsServer . listen ( SSL_PORT , '0.0.0.0' , ( ) => {
1192+ console . log ( `HTTPS server running on port ${ SSL_PORT } ` ) ;
1193+ console . log ( 'HTTPS: Enabled (self-signed certificate)' ) ;
1194+ } ) ;
1195+ httpsServer . on ( 'error' , ( err ) => {
1196+ console . error ( 'HTTPS server error:' , err ?. message || err ) ;
1197+ } ) ;
1198+ } catch ( err ) {
1199+ console . error ( 'Failed to start HTTPS server (self-signed):' , err ?. message || err ) ;
1200+ console . log ( 'HTTPS: Disabled due to error' ) ;
1201+ }
1202+ } else {
1203+ console . log ( 'HTTPS: Disabled' ) ;
1204+ }
11751205} ) ;
0 commit comments