|
| 1 | +import http from "http" |
| 2 | +import url from "url" |
| 3 | +import fs from "fs" |
| 4 | +import escape from "escape-html" |
| 5 | +import axios from "axios" |
| 6 | +import zlib from "zlib" |
| 7 | +import readline from "readline" |
| 8 | + |
| 9 | +const server = http.createServer((req, res) => { |
| 10 | + if (!req.url) { |
| 11 | + res.end("Invalid request") |
| 12 | + return |
| 13 | + } |
| 14 | + |
| 15 | + const parsedUrl = url.parse(req.url, true) |
| 16 | + const queryData = parsedUrl.query |
| 17 | + |
| 18 | + if (req.url.startsWith("/DaysBetweenDates")) { |
| 19 | + const date1 = queryData.date1 as string |
| 20 | + const date2 = queryData.date2 as string |
| 21 | + |
| 22 | + const date1_ms = Date.parse(date1) |
| 23 | + const date2_ms = Date.parse(date2) |
| 24 | + |
| 25 | + const difference_ms = date2_ms - date1_ms |
| 26 | + |
| 27 | + res.end(Math.round(difference_ms / 86400000) + " days") |
| 28 | + } else if (req.url.startsWith("/Validatephonenumber")) { |
| 29 | + const phoneNumber = queryData.phoneNumber as string |
| 30 | + |
| 31 | + const regex = /^(\+34|0034|34)?[ -]*(6|7)[ -]*([0-9][ -]*){8}$/ |
| 32 | + |
| 33 | + if (regex.test(phoneNumber)) { |
| 34 | + res.end("valid") |
| 35 | + } else { |
| 36 | + res.end("invalid") |
| 37 | + } |
| 38 | + } else if (req.url.startsWith("/ValidateSpanishDNI")) { |
| 39 | + const dni = queryData.dni as string |
| 40 | + |
| 41 | + const dniLetter = dni.charAt(dni.length - 1) |
| 42 | + const dniNumber = parseInt(dni.substring(0, dni.length - 1), 10) |
| 43 | + const dniLetterCalc = "TRWAGMYFPDXBNJZSQVHLCKE".charAt(dniNumber % 23) |
| 44 | + |
| 45 | + if (dniLetter === dniLetterCalc) { |
| 46 | + res.end("valid") |
| 47 | + } else { |
| 48 | + res.end("invalid") |
| 49 | + } |
| 50 | + } else if (req.url.startsWith("/ReturnColorCode")) { |
| 51 | + const colors = fs.readFileSync("colors.json", "utf-8") |
| 52 | + const colorsObj = JSON.parse(colors) |
| 53 | + |
| 54 | + const color = queryData.color as string |
| 55 | + let colorFound = "not found" |
| 56 | + |
| 57 | + for (let i = 0; i < colorsObj.length; i++) { |
| 58 | + if (colorsObj[i].color === color) { |
| 59 | + colorFound = colorsObj[i].code.hex |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + res.end(colorFound) |
| 65 | + } else if (req.url.startsWith("/SendEmail")) { |
| 66 | + // Implementation for SendEmail |
| 67 | + } else if (req.url.startsWith("/TellMeAJoke")) { |
| 68 | + axios |
| 69 | + .get("https://official-joke-api.appspot.com/random_joke") |
| 70 | + .then((response) => { |
| 71 | + res.end(response.data.setup + " " + response.data.punchline) |
| 72 | + }) |
| 73 | + .catch((error) => { |
| 74 | + console.log(error) |
| 75 | + }) |
| 76 | + } else if (req.url.startsWith("/MoviesByDirector")) { |
| 77 | + const director = queryData.director as string |
| 78 | + |
| 79 | + axios |
| 80 | + .get(`http://www.omdbapi.com/?apikey=XXXXXXX&s=${director}`) |
| 81 | + .then((response) => { |
| 82 | + let movies = "" |
| 83 | + for (let i = 0; i < response.data.Search.length; i++) { |
| 84 | + movies += response.data.Search[i].Title + ", " |
| 85 | + } |
| 86 | + res.end(movies) |
| 87 | + }) |
| 88 | + .catch((error) => { |
| 89 | + console.log(error) |
| 90 | + }) |
| 91 | + } else if (req.url.startsWith("/ParseUrl")) { |
| 92 | + const someUrl = queryData.someurl as string |
| 93 | + const urlObj = new URL(someUrl) |
| 94 | + |
| 95 | + const host = urlObj.host |
| 96 | + |
| 97 | + res.end("host: " + host) |
| 98 | + } else if (req.url.startsWith("/ListFiles")) { |
| 99 | + const currentDir = __dirname |
| 100 | + const files = fs.readdirSync(currentDir) |
| 101 | + |
| 102 | + res.end(escape(files.toString())) |
| 103 | + } else if (req.url.startsWith("/GetFullTextFile")) { |
| 104 | + const text = fs.readFileSync("sample.txt", "utf-8") |
| 105 | + const lines = text.split("\r") |
| 106 | + |
| 107 | + let linesFound = "" |
| 108 | + for (let i = 0; i < lines.length; i++) { |
| 109 | + if (lines[i].includes("Fusce")) { |
| 110 | + linesFound += lines[i] + ", " |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + res.end(linesFound) |
| 115 | + } else if (req.url.startsWith("/GetLineByLinefromtTextFile")) { |
| 116 | + const lineReader = readline.createInterface({ |
| 117 | + input: fs.createReadStream("sample.txt"), |
| 118 | + }) |
| 119 | + |
| 120 | + const promise = new Promise<string[]>((resolve, reject) => { |
| 121 | + const lines: string[] = [] |
| 122 | + lineReader.on("line", (line: string) => { |
| 123 | + if (line.includes("Fusce")) { |
| 124 | + lines.push(line) |
| 125 | + } |
| 126 | + }) |
| 127 | + lineReader.on("close", () => { |
| 128 | + resolve(lines) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + promise.then((lines) => { |
| 133 | + res.end(lines.toString()) |
| 134 | + }) |
| 135 | + } else if (req.url.startsWith("/CalculateMemoryConsumption")) { |
| 136 | + const memory = process.memoryUsage().heapUsed / 1024 / 1024 |
| 137 | + |
| 138 | + res.end(memory.toFixed(2) + " GB") |
| 139 | + } else if (req.url.startsWith("/MakeZipFile")) { |
| 140 | + const gzip = zlib.createGzip() |
| 141 | + const input = fs.createReadStream("sample.txt") |
| 142 | + const output = fs.createWriteStream("sample.gz") |
| 143 | + |
| 144 | + input.pipe(gzip).pipe(output) |
| 145 | + |
| 146 | + res.end("sample.gz created") |
| 147 | + } else if (req.url.startsWith("/RandomEuropeanCountry")) { |
| 148 | + const countries = [ |
| 149 | + { country: "Italy", iso: "IT" }, |
| 150 | + { country: "France", iso: "FR" }, |
| 151 | + { country: "Spain", iso: "ES" }, |
| 152 | + { country: "Germany", iso: "DE" }, |
| 153 | + { country: "United Kingdom", iso: "GB" }, |
| 154 | + { country: "Greece", iso: "GR" }, |
| 155 | + { country: "Portugal", iso: "PT" }, |
| 156 | + { country: "Romania", iso: "RO" }, |
| 157 | + { country: "Bulgaria", iso: "BG" }, |
| 158 | + { country: "Croatia", iso: "HR" }, |
| 159 | + { country: "Czech Republic", iso: "CZ" }, |
| 160 | + { country: "Denmark", iso: "DK" }, |
| 161 | + { country: "Estonia", iso: "EE" }, |
| 162 | + { country: "Finland", iso: "FI" }, |
| 163 | + { country: "Hungary", iso: "HU" }, |
| 164 | + { country: "Ireland", iso: "IE" }, |
| 165 | + { country: "Latvia", iso: "LV" }, |
| 166 | + { country: "Lithuania", iso: "LT" }, |
| 167 | + { country: "Luxembourg", iso: "LU" }, |
| 168 | + { country: "Malta", iso: "MT" }, |
| 169 | + { country: "Netherlands", iso: "NL" }, |
| 170 | + { country: "Poland", iso: "PL" }, |
| 171 | + { country: "Slovakia", iso: "SK" }, |
| 172 | + { country: "Slovenia", iso: "SI" }, |
| 173 | + { country: "Sweden", iso: "SE" }, |
| 174 | + { country: "Belgium", iso: "BE" }, |
| 175 | + { country: "Austria", iso: "AT" }, |
| 176 | + { country: "Switzerland", iso: "CH" }, |
| 177 | + { country: "Cyprus", iso: "CY" }, |
| 178 | + { country: "Iceland", iso: "IS" }, |
| 179 | + { country: "Norway", iso: "NO" }, |
| 180 | + { country: "Albania", iso: "AL" }, |
| 181 | + { country: "Andorra", iso: "AD" }, |
| 182 | + { country: "Armenia", iso: "AM" }, |
| 183 | + { country: "Azerbaijan", iso: "AZ" }, |
| 184 | + { country: "Belarus", iso: "BY" }, |
| 185 | + { country: "Bosnia and Herzegovina", iso: "BA" }, |
| 186 | + { country: "Georgia", iso: "GE" }, |
| 187 | + { country: "Kazakhstan", iso: "KZ" }, |
| 188 | + { country: "Kosovo", iso: "XK" }, |
| 189 | + { country: "Liechtenstein", iso: "LI" }, |
| 190 | + { country: "Macedonia", iso: "MK" }, |
| 191 | + { country: "Moldova", iso: "MD" }, |
| 192 | + { country: "Monaco", iso: "MC" }, |
| 193 | + { country: "Montenegro", iso: "ME" }, |
| 194 | + { country: "Russia", iso: "RU" }, |
| 195 | + { country: "San Marino", iso: "SM" }, |
| 196 | + { country: "Serbia", iso: "RS" }, |
| 197 | + { country: "Turkey", iso: "TR" }, |
| 198 | + { country: "Ukraine", iso: "UA" }, |
| 199 | + { country: "Vatican City", iso: "VA" }, |
| 200 | + ] |
| 201 | + |
| 202 | + const randomCountry = countries[Math.floor(Math.random() * countries.length)] |
| 203 | + |
| 204 | + res.end(randomCountry.country + " " + randomCountry.iso) |
| 205 | + } else if (req.url.startsWith("/Get")) { |
| 206 | + const key = queryData.key as string |
| 207 | + |
| 208 | + if (!key) { |
| 209 | + res.end("key not passed") |
| 210 | + } else { |
| 211 | + res.end("hello " + escape(key)) |
| 212 | + } |
| 213 | + } else { |
| 214 | + res.end("Called method not found") |
| 215 | + } |
| 216 | +}) |
| 217 | + |
| 218 | +server.listen(3000, () => { |
| 219 | + console.log("server is listening on port 3000") |
| 220 | +}) |
0 commit comments