added session so multiple people can use it at once

This commit is contained in:
valentijn
2024-01-25 14:29:09 +01:00
parent f6e8eda33c
commit fe674c9e06
4 changed files with 183 additions and 138 deletions

103
server.js
View File

@@ -1,56 +1,71 @@
const express = require('express')
const app = express()
const port = 3000
const express = require('express');
const session = require('express-session');
const app = express();
const port = 3000;
const commands = require("./commands/commands")
const commands = require("./commands/commands");
app.use(express.static("httpdocs/public"))
app.use(express.static("httpdocs/public"));
const documentRoot = `${__dirname}/httpdocs`
const documentRoot = `${__dirname}/httpdocs`;
// Session configuration
app.use(session({
secret: 'your-secret', // Change this to a secret phrase
resave: false,
saveUninitialized: true,
cookie: { secure: false } // Set to true if using https
}));
app.get('/', (req, res) => {
res.sendFile(documentRoot+"/index.html")
})
if (!req.session.state) {
req.session.state = {
"username": "guest",
"directory": "/"
};
}
res.sendFile(documentRoot + "/index.html");
});
app.get("/execute", (req, res)=> {
const raw_command = req.query.command;
if (raw_command == ""){
commands.customTextResponse(res, "");
return;
}a
app.get("/execute", (req, res) => {
const raw_command = req.query.command;
if (raw_command == ""){
commands.customTextResponse(req, res, "");
return;
}
const formatted_command = raw_command.trim();
const command = formatted_command.split(" ")[0]
const formatted_command = raw_command.trim();
const command = formatted_command.split(" ")[0];
switch(command){
default:
commands.customTextResponse(res, "Command '"+command+"' not found, please try again later, or type 'help' for the list of available commands");
break;
case "ls":
commands.ls(res);
break;
case "whoami":
commands.whoami(res);
break;
case "cd":
commands.cd(res, formatted_command);
break;
case "pwd":
commands.pwd(res);
break;
case "cat":
commands.cat(res, formatted_command )
break;
case "help":
commands.help(res);
}
})
switch(command){
case "ls":
commands.ls(req, res);
break;
case "whoami":
commands.whoami(req, res);
break;
case "cd":
commands.cd(req, res, formatted_command);
break;
case "pwd":
commands.pwd(req, res);
break;
case "cat":
commands.cat(req, res, formatted_command);
break;
case "help":
commands.help(req, res);
break;
default:
commands.customTextResponse(req, res, "Command '" + command + "' not found, please try again later, or type 'help' for the list of available commands");
break;
}
});
app.get("/currentDir", (req, res)=>{
res.send(commands.getCurrentDir());
})
app.get("/currentDir", (req, res) => {
res.send(commands.getCurrentDir(req.session.state));
});
app.listen(port, () => {
console.log(`Epic Terminal App listening on port ${port}`)
})
console.log(`Epic Terminal App listening on port ${port}`);
});