Files
Terminal-Website/server.js
2024-01-19 12:10:11 +01:00

58 lines
1.3 KiB
JavaScript

const express = require('express')
const app = express()
const port = 3000
const commands = require("./commands/commands")
app.use(express.static("httpdocs/public"))
const documentRoot = `${__dirname}/httpdocs`
let fileSystem = {
"/": {
"file1.txt": "this is the content of file1",
"projects": "dir"
},
"/projects/": {
"file2.txt": "this is file 2 :0",
"file3.txt": "and file 3 :D"
}
}
app.get('/', (req, res) => {
res.sendFile(documentRoot+"/index.html")
})
app.get("/execute", (req, res)=> {
const raw_command = req.query.command;
if (raw_command == ""){
res.json({"response": ""});
return;
}
const formatted_command = String(raw_command).trim();
const currentDir = req.query.currentDir
if (formatted_command.startsWith("ls")){
commands.ls(req, res, currentDir, fileSystem);
}
else if (formatted_command.startsWith("whoami")){
commands.whoami(req, res);
}
else if (formatted_command.startsWith("pwd")){
commands.pwd(req, res, currentDir);
}
else if (formatted_command.startsWith("help"))
{
commands.help(req, res);
}
else {
res.json({"response": "Unknown command, please try again later, or run 'help' for a list of commands"});
}
})
app.listen(port, () => {
console.log(`Epic Terminal App listening on port ${port}`)
})