-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (51 loc) · 1.59 KB
/
Copy pathserver.js
File metadata and controls
59 lines (51 loc) · 1.59 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
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// Command handler
app.post('/api/command', async (req, res) => {
const { command } = req.body;
if (!command) {
return res.status(400).json({ error: 'Command is required' });
}
try {
// Handle built-in commands
if (command === '/help') {
res.json({
type: 'response',
message: 'Available commands:\n /help - Show this help message\n /clear - Clear the terminal\n /time - Show current time\n /echo <text> - Echo back text'
});
} else if (command === '/clear') {
res.json({ type: 'clear' });
} else if (command === '/time') {
res.json({
type: 'response',
message: new Date().toLocaleString()
});
} else if (command.startsWith('/echo ')) {
res.json({
type: 'response',
message: command.substring(6)
});
} else {
res.json({
type: 'response',
message: `Unknown command: ${command}\nType /help for available commands`
});
}
} catch (error) {
console.error('[API] Error processing command:', error);
res.status(500).json({ error: 'Error processing command' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
if (require.main === module) {
app.listen(PORT, () => {
console.log(`[Server] Terminal CLI running on http://localhost:${PORT}`);
});
}
module.exports = app;