-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimal_fun.js
84 lines (74 loc) · 2.14 KB
/
animal_fun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const fs = require('fs')
const http = require('http')
const qs = require('querystring')
const cache = {}
// fs.readFile('./animals.txt', 'utf-8', (err, data) => {
// if (err) {
// console.log(err)
// return
// }
// console.log(data)
// })
// fs.writeFile('./example.txt', 'I will be written to example.txt', err => {
// if (err) {
// console.log(err)
// return
// }
// console.log('file successfully written')
// })
// function selectAnimals(animalString, animalLetter) {
// return animalString
// .split('\n')
// .filter(animal => animal.startsWith(animalLetter))
// .join('\n')
// }
// const animalLetter = process.argv[2].toUpperCase()
// fs.readFile('./animals.txt', 'utf-8', (err, data) => {
// if (err) {
// console.log(err)
// return
// }
// const animals = selectAnimals(data, animalLetter)
// fs.writeFile(`${animalLetter}_animals.txt`, animals, err => {
// if (err) {
// console.log(err)
// return
// }
// console.log(`successfully created ${animalLetter}_animals.txt`)
// })
// })
// const animalServer = http.createServer((req, res) => {
// const query = req.url.split('?')[1]
// if (query !== undefined) {
// const animalLetter = qs.parse(query).letter.toUpperCase()
// if (cache[animalLetter] !== undefined) {
// res.end(cache[animalLetter])
// }
// if (animalLetter !== undefined) {
// fs.readFile('./animals.txt', 'utf-8', (err, data) => {
// if (err) {
// console.log(err)
// res.end('IT WENT POORLY')
// return
// }
// const animals = selectAnimals(data, animalLetter)
// cache[animalLetter] = animals
// res.end(animals)
// })
// }
// } else {
// if (cache['animals'] !== undefined) {
// res.end(cache['animals'])
// }
// fs.readFile('./animals.txt', 'utf-8', (err, data) => {
// if (err) {
// console.log(err)
// res.end('IT WENT POORLY')
// return
// }
// cache['animals'] = data
// res.end(data)
// })
// }
// })
// animalServer.listen(8000, () => console.log("I'm listening on port 8000"))