-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
123 lines (103 loc) · 3.35 KB
/
dev-server.js
File metadata and controls
123 lines (103 loc) · 3.35 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 8080;
// Load configuration from .env file
function loadEnvFile() {
const envPath = path.join(__dirname, '.env');
const config = {};
if (fs.existsSync(envPath)) {
const envContent = fs.readFileSync(envPath, 'utf8');
const lines = envContent.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('#')) {
const [key, ...valueParts] = trimmed.split('=');
if (key && valueParts.length > 0) {
config[key.trim()] = valueParts.join('=').trim();
}
}
}
}
return config;
}
const config = loadEnvFile();
// Build the application using the same process as production
function buildApplication() {
console.log('🔧 Building application...');
const { execSync } = require('child_process');
try {
// Use the same build command as production
execSync('npm run build', { stdio: 'inherit' });
console.log('✅ Build complete');
} catch (error) {
console.error('❌ Build failed:', error.message);
process.exit(1);
}
}
// Build on startup
buildApplication();
// MIME types
const mimeTypes = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.json': 'application/json'
};
const server = http.createServer((req, res) => {
console.log(`${req.method} ${req.url}`);
// Handle root path - serve from dist directory (same as production)
if (req.url === '/') {
const htmlPath = path.join(__dirname, 'dist', 'index.html');
fs.readFile(htmlPath, 'utf8', (err, content) => {
if (err) {
res.writeHead(500);
res.end('Error loading index.html from dist directory');
return;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content);
});
return;
}
// Serve all other files from dist directory (same as production)
let filePath = path.join(__dirname, 'dist', req.url);
const extname = path.extname(filePath);
const contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, (err, content) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end('<h1>404 Not Found</h1>');
} else {
res.writeHead(500);
res.end(`Server Error: ${err.code}`);
}
} else {
res.writeHead(200, {
'Content-Type': contentType,
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
});
res.end(content);
}
});
});
server.listen(PORT, () => {
console.log('🚀 Development Server Started!');
console.log(`📱 Frontend: http://localhost:${PORT}`);
if (config.ROWT_API_ENDPOINT) {
console.log(`🔗 API Endpoint: ${config.ROWT_API_ENDPOINT}`);
console.log('✅ Configuration loaded from .env file');
} else {
console.log('⚠️ No .env file found - application will use fallback configuration');
console.log(' Create a .env file with ROWT_API_ENDPOINT=your-server-url');
}
console.log('');
console.log('Open http://localhost:8080 in your browser');
});