Skip to content

Commit f8bacf1

Browse files
JoostVoskuilcmendible
authored andcommitted
Add complete solution
1 parent dfdd743 commit f8bacf1

File tree

9 files changed

+659
-1
lines changed

9 files changed

+659
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ build/Release
4343
node_modules/
4444
jspm_packages/
4545

46+
# NPM lock file
47+
package-lock.json
48+
4649
# Snowpack dependency directory (https://snowpack.dev/)
4750
web_modules/
4851

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[D|d]ockerfile
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Create a dockerfile with node image
2+
FROM node:latest
3+
4+
# Create a directory to hold the application code inside the image, this will be the working directory for your application
5+
RUN mkdir -p /usr/src/app
6+
7+
# Set the working directory to /usr/src/app
8+
WORKDIR /usr/src/app
9+
10+
# Copy package.json and package-lock.json to the working directory
11+
COPY package*.json ./
12+
13+
# Install npm
14+
RUN npm install
15+
16+
# Copy the current directory contents into the container at /usr/src/app
17+
COPY . .
18+
19+
# Make port 3000 available to the world outside this container
20+
EXPOSE 3000
21+
22+
# Run Nodeserver.js when the container launches
23+
CMD ["node", "nodeserver.js"]
24+
25+
# Write a docker comand to build the image and tag it as mynodeapp
26+
#docker build -t mynodeapp .
27+
28+
# Write command to run docker in port 4000
29+
#docker run -p 4000:3000 -d <image id>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[
2+
{
3+
"color": "black",
4+
"category": "hue",
5+
"type": "primary",
6+
"code": {
7+
"rgba": [255,255,255,1],
8+
"hex": "#000000"
9+
}
10+
},
11+
{
12+
"color": "white",
13+
"category": "value",
14+
"code": {
15+
"rgba": [0,0,0,1],
16+
"hex": "#FFFFFF"
17+
}
18+
},
19+
{
20+
"color": "red",
21+
"category": "hue",
22+
"type": "primary",
23+
"code": {
24+
"rgba": [255,0,0,1],
25+
"hex": "#FF0000"
26+
}
27+
},
28+
{
29+
"color": "blue",
30+
"category": "hue",
31+
"type": "primary",
32+
"code": {
33+
"rgba": [0,0,255,1],
34+
"hex": "#0000FF"
35+
}
36+
},
37+
{
38+
"color": "yellow",
39+
"category": "hue",
40+
"type": "primary",
41+
"code": {
42+
"rgba": [255,255,0,1],
43+
"hex": "#FFFF00"
44+
}
45+
},
46+
{
47+
"color": "green",
48+
"category": "hue",
49+
"type": "secondary",
50+
"code": {
51+
"rgba": [0,255,0,1],
52+
"hex": "#00FF00"
53+
}
54+
}
55+
]
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "nodeserver",
3+
"version": "1.0.0",
4+
"main": "nodeserver.js",
5+
"scripts": {
6+
"build": "tsc",
7+
"start": "node nodeserver.js",
8+
"test": "mocha test.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"description": "",
14+
"dependencies": {
15+
"axios": "^1.7.7",
16+
"escape-html": "^1.0.3",
17+
"typescript": "^5.6.3"
18+
},
19+
"devDependencies": {
20+
"@types/escape-html": "^1.0.4",
21+
"@types/mocha": "^10.0.9",
22+
"@types/node": "^22.8.6",
23+
"mocha": "^10.8.2"
24+
}
25+
}

0 commit comments

Comments
 (0)