-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
112 lines (91 loc) · 2.99 KB
/
Copy pathstart.js
File metadata and controls
112 lines (91 loc) · 2.99 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
import { spawn, exec } from 'child_process';
import path from 'path';
import fs from 'fs';
import os from 'os';
import { fileURLToPath } from 'url';
// Get __dirname equivalent in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Configuration
const SERVER_PORT = 5000;
const FRONTEND_PORT = 3000;
// Function to check if MongoDB is already running
function checkMongoDBRunning(callback) {
const command = os.platform() === 'win32'
? 'netstat -ano | findstr "LISTENING" | findstr "27017"'
: 'lsof -i:27017';
exec(command, (error, stdout) => {
callback(!error && stdout.trim() !== '');
});
}
// Create a promise-based version of the check
function isMongoDBRunning() {
return new Promise((resolve) => {
checkMongoDBRunning(isRunning => {
resolve(isRunning);
});
});
}
// Function to start Express server
function startExpressServer() {
console.log('Starting Express server...');
const serverProcess = spawn('node', ['server.js'], {
stdio: 'inherit'
});
serverProcess.on('error', (error) => {
console.error('Failed to start Express server:', error);
process.exit(1);
});
return serverProcess;
}
// Function to start frontend dev server
function startFrontendDevServer() {
console.log('Starting frontend development server...');
const npmCmd = os.platform() === 'win32' ? 'npm.cmd' : 'npm';
const frontendProcess = spawn(npmCmd, ['run', 'dev'], {
stdio: 'inherit',
shell: true
});
frontendProcess.on('error', (error) => {
console.error('Failed to start frontend server:', error);
});
return frontendProcess;
}
// Main function
async function main() {
console.log('Starting ICD-10-CM Browser application...');
// Check MongoDB
const mongoRunning = await isMongoDBRunning();
if (!mongoRunning) {
console.warn('\x1b[33mWARNING: MongoDB does not appear to be running!\x1b[0m');
console.warn('\x1b[33mPlease ensure MongoDB is installed and running on port 27017\x1b[0m');
console.warn('\x1b[33mOn Windows, check services.msc for MongoDB Service\x1b[0m');
console.warn('\x1b[33mOn macOS/Linux, run: sudo systemctl start mongodb\x1b[0m');
console.warn('\x1b[33mContinuing anyway, but the application may not work correctly...\x1b[0m\n');
} else {
console.log('\x1b[32mMongoDB is running. Good!\x1b[0m');
}
// Start Express server
const serverProcess = startExpressServer();
// Start frontend dev server
const frontendProcess = startFrontendDevServer();
// Handle application shutdown
const cleanup = () => {
console.log('\nShutting down...');
if (serverProcess) {
serverProcess.kill();
}
if (frontendProcess) {
frontendProcess.kill();
}
process.exit(0);
};
// Handle process termination
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
}
// Run the main function
main().catch(error => {
console.error('Error starting application:', error);
process.exit(1);
});