diff --git a/package.json b/package.json index b90edfd..ed10534 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ -{ +{ + "dependencies": { "chalk": "^5.0.0", "prompt-sync": "^4.2.0" diff --git a/run.js b/run.js index 752bb42..79f758d 100644 --- a/run.js +++ b/run.js @@ -1,23 +1,55 @@ -const prompt = require("prompt-sync") -const chalk = require("chalk") -const path = require('path'); -const warn = function(message, color){ - if (!color) color = red - console.log(chalk[color](message)) -} - +let prompt = require("prompt-sync")() +let chalk = import("chalk") +let path = require("path") +let fs = require("fs") +let log = console.log; +let http = require("http") -if (path.existsSync("/data")) { // or fs.existsSync - warn("WARNING: Folder \"data\" will be erased and rebuilt if you proceed") +if (fs.existsSync("data")) { + console.log("WARNING: Folder \"data\" will be erased and rebuilt if you proceed") const answer = prompt("Would you like to proceed? (S/N)") - if (String(answer).toUpperCase=="S"){ - //delete - console.log("Done") - } else if (String(answer).toUpperCase=="N"){ + if (answer.toUpperCase() =="S"){ + log("removing") + fs.rmdirSync("data",{recursive: true}) + console.log("Done!") + console.log("Now we can start the HTTP server lol") + + } else if (answer.toUpperCase() =="N"){ console.log("Goodbye!") - exit(0) + process.exit(0) }else{ - warn("Not a valid answer. Exiting...") - exit(1) + console.log("Not a valid answer. Exiting...") + process.exit(1) } } + +const requestListener = function (req, res) { + log(req.url) + if (req.url === "/test"){ + fs.readFile("templates/rick.html", function (err,data) { + if (err) { + res.writeHead(404); + res.end(JSON.stringify(err)); + return; + } + res.writeHead(200); + res.end(data); + }); + }else if (req.url === "/"){ + + }else { + fs.readFile("static" + req.url, function (err,data) { + if (err) { + res.writeHead(404); + res.end(JSON.stringify(err)); + return; + } + res.writeHead(200); + res.end(data); + }); + } +} + +const server = http.createServer(requestListener); +log("Server created. Starting...") +server.listen(80); diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 0000000..636f7c0 Binary files /dev/null and b/static/favicon.ico differ diff --git a/templates/rick.html b/templates/rick.html new file mode 100644 index 0000000..394802b --- /dev/null +++ b/templates/rick.html @@ -0,0 +1,10 @@ + + +Test + + + + + \ No newline at end of file