slicejs-web-framework 1.0.34 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/index.js +98 -46
- package/package.json +1 -1
package/api/index.js
CHANGED
|
@@ -11,89 +11,141 @@ import sliceConfig from '../src/sliceConfig.json' with { type: 'json' };
|
|
|
11
11
|
let server;
|
|
12
12
|
|
|
13
13
|
const app = express();
|
|
14
|
-
const PORT = 3001;
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
const
|
|
15
|
+
// Detectar configuración automáticamente
|
|
16
|
+
const PORT = process.env.PORT || 3001;
|
|
17
|
+
const NODE_ENV = process.env.NODE_ENV || 'development';
|
|
18
|
+
const IS_CLI_MODE = process.env.SLICE_CLI_MODE === 'true';
|
|
19
|
+
|
|
20
|
+
// Determinar directorio a servir
|
|
21
|
+
// Prioridad: 1) NODE_ENV del CLI, 2) sliceConfig.production, 3) default 'src'
|
|
22
|
+
let folderDeployed;
|
|
23
|
+
if (NODE_ENV === 'production') {
|
|
24
|
+
folderDeployed = 'dist';
|
|
25
|
+
} else if (NODE_ENV === 'development') {
|
|
26
|
+
folderDeployed = 'src';
|
|
27
|
+
} else {
|
|
28
|
+
// Fallback al comportamiento original
|
|
29
|
+
const isProduction = sliceConfig.production === true;
|
|
30
|
+
folderDeployed = isProduction ? 'dist' : 'src';
|
|
31
|
+
}
|
|
18
32
|
|
|
19
33
|
// Servir archivos estáticos desde la carpeta 'Slice'
|
|
20
34
|
app.use('/Slice/', express.static(path.join(__dirname, '..', 'node_modules', 'slicejs-web-framework', 'Slice')));
|
|
21
|
-
// Servir archivos estáticos desde la carpeta 'App'
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
35
|
|
|
36
|
+
// Testing routes (mantener las existentes)
|
|
27
37
|
app.get('/testing1', (req, res) => {
|
|
28
38
|
res.send(` Actual route in server: __dirname: ${__dirname} __filename: ${__filename} - checking if file exists: ${path.join(__dirname, '..', 'src','App', 'index.html')}`);
|
|
29
39
|
});
|
|
30
40
|
|
|
31
|
-
|
|
41
|
+
// Servir archivos estáticos desde la carpeta determinada (src o dist)
|
|
32
42
|
app.use(express.static(path.join(__dirname,'..', folderDeployed)));
|
|
33
43
|
|
|
34
|
-
|
|
35
44
|
app.get('/testing2', (req, res) => {
|
|
36
45
|
res.send(` Actual route in server: __dirname: ${__dirname} __filename: ${__filename} - checking if file exists: ${path.join(__dirname, '..', 'src','App', 'index.html')}`);
|
|
37
46
|
});
|
|
38
47
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
// API de estado (útil para debugging)
|
|
49
|
+
app.get('/api/status', (req, res) => {
|
|
50
|
+
res.json({
|
|
51
|
+
mode: NODE_ENV,
|
|
52
|
+
serving: folderDeployed,
|
|
53
|
+
port: PORT,
|
|
54
|
+
cliMode: IS_CLI_MODE,
|
|
55
|
+
timestamp: new Date().toISOString()
|
|
56
|
+
});
|
|
57
|
+
});
|
|
43
58
|
|
|
44
|
-
// Ruta para servir el index.html desde la carpeta
|
|
59
|
+
// Ruta para servir el index.html desde la carpeta apropiada
|
|
45
60
|
app.get('*', (req, res) => {
|
|
46
|
-
const filePath = path.join(__dirname, '..',
|
|
47
|
-
res.sendFile(filePath)
|
|
61
|
+
const filePath = path.join(__dirname, '..', folderDeployed, 'App', 'index.html');
|
|
62
|
+
res.sendFile(filePath, (err) => {
|
|
63
|
+
if (err) {
|
|
64
|
+
// Fallback si no existe App/index.html
|
|
65
|
+
const fallbackPath = path.join(__dirname, '..', folderDeployed, 'index.html');
|
|
66
|
+
res.sendFile(fallbackPath, (fallbackErr) => {
|
|
67
|
+
if (fallbackErr) {
|
|
68
|
+
res.status(404).send(`File not found: ${folderDeployed}/index.html`);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
48
73
|
});
|
|
49
74
|
|
|
50
75
|
function startServer() {
|
|
51
76
|
server = app.listen(PORT, () => {
|
|
52
|
-
|
|
77
|
+
if (IS_CLI_MODE) {
|
|
78
|
+
// Modo CLI - salida simple y limpia (sin menú)
|
|
79
|
+
console.log(`🚀 Slice.js ${NODE_ENV} server running at http://localhost:${PORT}`);
|
|
80
|
+
console.log(`📁 Serving files from /${folderDeployed} directory`);
|
|
81
|
+
console.log('Press Ctrl+C to stop the server');
|
|
82
|
+
} else {
|
|
83
|
+
// Modo standalone - mostrar menú interactivo
|
|
84
|
+
showMenu();
|
|
85
|
+
}
|
|
53
86
|
});
|
|
54
87
|
}
|
|
55
88
|
|
|
56
89
|
async function showMenu() {
|
|
57
|
-
|
|
58
90
|
console.clear();
|
|
59
91
|
console.log("\n=================================");
|
|
60
92
|
console.log(" SLICE SERVER MENU ");
|
|
61
93
|
console.log("=================================\n");
|
|
62
94
|
|
|
63
95
|
const url = `http://localhost:${PORT}`;
|
|
64
|
-
|
|
96
|
+
console.log(`Server is running on port ${PORT}, ${url}`);
|
|
97
|
+
console.log(`Mode: ${NODE_ENV} | Serving: /${folderDeployed}\n`);
|
|
65
98
|
|
|
66
99
|
while (true) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
100
|
+
try {
|
|
101
|
+
const { action } = await inquirer.prompt([
|
|
102
|
+
{
|
|
103
|
+
type: 'list',
|
|
104
|
+
name: 'action',
|
|
105
|
+
message: 'Select an option:',
|
|
106
|
+
choices: ['Restart Server', 'Stop Server (Exit)']
|
|
107
|
+
}
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
if (action === 'Stop Server (Exit)') {
|
|
111
|
+
console.log('\nShutting down server...');
|
|
112
|
+
server.close(() => {
|
|
113
|
+
console.log('Server stopped.');
|
|
114
|
+
process.exit(0);
|
|
115
|
+
});
|
|
116
|
+
break;
|
|
117
|
+
} else if (action === 'Restart Server') {
|
|
118
|
+
console.log('\nRestarting server...');
|
|
119
|
+
server.close(() => {
|
|
120
|
+
console.log('Server stopped. Restarting...');
|
|
121
|
+
startServer();
|
|
122
|
+
});
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
} catch (error) {
|
|
126
|
+
// Si hay error con inquirer, continuar sin menú
|
|
127
|
+
console.log('\n💡 Interactive menu not available - Press Ctrl+C to stop');
|
|
128
|
+
break;
|
|
90
129
|
}
|
|
91
130
|
}
|
|
92
131
|
}
|
|
93
132
|
|
|
94
|
-
|
|
95
|
-
|
|
133
|
+
// Manejar cierre del proceso
|
|
134
|
+
process.on('SIGINT', () => {
|
|
135
|
+
if (IS_CLI_MODE) {
|
|
136
|
+
console.log('\n🛑 Server stopped');
|
|
137
|
+
} else {
|
|
138
|
+
console.log('\n🛑 Slice server stopped');
|
|
139
|
+
}
|
|
140
|
+
process.exit(0);
|
|
141
|
+
});
|
|
96
142
|
|
|
97
|
-
|
|
143
|
+
process.on('SIGTERM', () => {
|
|
144
|
+
console.log('\n🛑 Server terminated');
|
|
145
|
+
process.exit(0);
|
|
146
|
+
});
|
|
98
147
|
|
|
148
|
+
// Iniciar servidor
|
|
149
|
+
startServer();
|
|
99
150
|
|
|
151
|
+
export default app;
|