97 lines
2.7 KiB
JavaScript
97 lines
2.7 KiB
JavaScript
import https from 'https';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const options = {
|
|
key: fs.readFileSync('./localhost+2-key.pem'),
|
|
cert: fs.readFileSync('./localhost+2.pem')
|
|
};
|
|
|
|
// MIME types mapping
|
|
const mimeTypes = {
|
|
'.html': 'text/html; charset=utf-8',
|
|
'.htm': 'text/html; charset=utf-8',
|
|
'.js': 'application/javascript; charset=utf-8',
|
|
'.mjs': 'application/javascript; charset=utf-8',
|
|
'.css': 'text/css; charset=utf-8',
|
|
'.json': 'application/json; charset=utf-8',
|
|
'.png': 'image/png',
|
|
'.jpg': 'image/jpeg',
|
|
'.jpeg': 'image/jpeg',
|
|
'.gif': 'image/gif',
|
|
'.svg': 'image/svg+xml',
|
|
'.ico': 'image/x-icon',
|
|
'.woff': 'font/woff',
|
|
'.woff2': 'font/woff2',
|
|
'.ttf': 'font/ttf',
|
|
'.eot': 'application/vnd.ms-fontobject'
|
|
};
|
|
|
|
const server = https.createServer(options, (req, res) => {
|
|
console.log(`📥 ${req.method} ${req.url}`);
|
|
|
|
let filePath = path.join(__dirname, 'dist', req.url === '/' ? 'index.html' : req.url);
|
|
|
|
// Security check - prevent directory traversal
|
|
if (!filePath.startsWith(path.join(__dirname, 'dist'))) {
|
|
res.writeHead(403);
|
|
res.end('Forbidden');
|
|
return;
|
|
}
|
|
|
|
// Check if file exists
|
|
fs.stat(filePath, (err, stats) => {
|
|
if (err || !stats.isFile()) {
|
|
// File not found, serve index.html for SPA routing
|
|
const indexPath = path.join(__dirname, 'dist', 'index.html');
|
|
fs.readFile(indexPath, (err, data) => {
|
|
if (err) {
|
|
res.writeHead(404);
|
|
res.end('File not found');
|
|
return;
|
|
}
|
|
res.writeHead(200, {
|
|
'Content-Type': 'text/html; charset=utf-8',
|
|
'Cache-Control': 'no-cache'
|
|
});
|
|
res.end(data);
|
|
});
|
|
return;
|
|
}
|
|
|
|
// Read and serve the file
|
|
fs.readFile(filePath, (err, data) => {
|
|
if (err) {
|
|
console.error('Error reading file:', err);
|
|
res.writeHead(500);
|
|
res.end('Server Error');
|
|
return;
|
|
}
|
|
|
|
const ext = path.extname(filePath);
|
|
const contentType = mimeTypes[ext] || 'application/octet-stream';
|
|
|
|
// Set cache headers for static assets
|
|
const cacheControl = ext.match(/\.(css|js|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf)$/)
|
|
? 'public, max-age=31536000'
|
|
: 'no-cache';
|
|
|
|
res.writeHead(200, {
|
|
'Content-Type': contentType,
|
|
'Cache-Control': cacheControl
|
|
});
|
|
res.end(data);
|
|
});
|
|
});
|
|
});
|
|
|
|
server.listen(3443, '0.0.0.0', () => {
|
|
console.log('🚀 Production HTTPS Server running on https://159.195.9.107:3443');
|
|
console.log('📁 Serving files from: ./dist/');
|
|
console.log('🔒 SSL Certificate: localhost+2.pem');
|
|
});
|