-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-server.js
More file actions
138 lines (120 loc) · 3.95 KB
/
simple-server.js
File metadata and controls
138 lines (120 loc) · 3.95 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Simple standalone server for Azure App Service without Express dependency
// Uses only Node.js built-in modules to avoid dependency issues
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
// Debug environment info
console.log('🔧 SIMPLE SERVER STARTUP DEBUG');
console.log('=== Environment Info ===');
console.log('Node version:', process.version);
console.log('Platform:', process.platform);
console.log('Working directory:', process.cwd());
console.log('Environment variables:');
console.log('- PORT:', process.env.PORT);
console.log('- NODE_ENV:', process.env.NODE_ENV);
console.log('- PWD:', process.env.PWD);
// Check file system
console.log('\n=== File System Check ===');
try {
const files = fs.readdirSync('.');
console.log('Current directory files:', files.slice(0, 10));
// Check for dist directory
if (fs.existsSync('dist')) {
const distFiles = fs.readdirSync('dist');
console.log('Dist directory files:', distFiles.slice(0, 10));
} else {
console.log('❌ Dist directory not found');
}
} catch (error) {
console.error('Error reading file system:', error.message);
}
const PORT = process.env.PORT || 8080;
const DIST_DIR = path.join(process.cwd(), 'dist');
// MIME types for static files
const mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon'
};
function getContentType(filePath) {
const ext = path.extname(filePath).toLowerCase();
return mimeTypes[ext] || 'application/octet-stream';
}
function serveStaticFile(req, res, filePath) {
const fullPath = path.join(DIST_DIR, filePath);
fs.readFile(fullPath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
// File not found, serve index.html for SPA routing
fs.readFile(path.join(DIST_DIR, 'index.html'), (indexErr, indexContent) => {
if (indexErr) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('404 - File Not Found');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(indexContent);
}
});
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('500 - Internal Server Error');
}
} else {
res.writeHead(200, { 'Content-Type': getContentType(filePath) });
res.end(content);
}
});
}
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
let pathname = parsedUrl.pathname;
console.log(`${new Date().toISOString()} ${req.method} ${pathname}`);
// Health check endpoint
if (pathname === '/health' || pathname === '/api/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memory: process.memoryUsage(),
env: process.env.NODE_ENV || 'development'
}));
return;
}
// Remove leading slash
if (pathname.startsWith('/')) {
pathname = pathname.slice(1);
}
// Default to index.html for root path
if (pathname === '' || pathname === '/') {
pathname = 'index.html';
}
serveStaticFile(req, res, pathname);
});
server.listen(PORT, () => {
console.log(`\n🚀 Simple server running on port ${PORT}`);
console.log(`Health check: http://localhost:${PORT}/health`);
console.log(`Serving static files from: ${DIST_DIR}`);
});
// Handle graceful shutdown
process.on('SIGTERM', () => {
console.log('Received SIGTERM, shutting down gracefully');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});
process.on('SIGINT', () => {
console.log('Received SIGINT, shutting down gracefully');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});