-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (55 loc) · 1.67 KB
/
server.js
File metadata and controls
66 lines (55 loc) · 1.67 KB
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
const http = require("http");
const _ = require('lodash')
const PORT = process.env.PORT || 3000;
const server = http.createServer((req, res) => {
if (req.url === "/") return respondHello(req, res);
if (req.url === "/user-agent") return respondU(req, res);
if (req.url.match(/^\/b64\//)) return respondB(req, res);
if (req.url === '/repetitive-word') return respondRepetitiveWord(req, res)
res.end();
});
function respondHello(req, res) {
res.end(JSON.stringify({ msg: "hello" }));
}
function respondU(req, res) {
const ua = req.headers["user-agent"];
res.end(JSON.stringify({ ua }));
}
function respondB(req, res) {
res.end(JSON.stringify({b64: Buffer.from(req.url.replace(/^\/b64\//, "")).toString("base64")}));
}
function respondRepetitiveWord (req, res) {
let body = ''
req.on('data', chunk => {
body += chunk.toString()
})
req.on('end', () => {
const words = countWords(JSON.parse(body).text)
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(words))
})
}
server.listen(PORT);
console.log(`Server listening on port ${PORT}`);
if (require.main !== module) module.exports = server;
function countWords (text) {
const parsedText = {}
const words = text.replace(/[,.!?:;"'()<>]/g, '').split(' ')
const repetitiveWords = []
words.forEach(word => {
if (word.length < 3 ||
word === 'the') return
parsedText.hasOwnProperty(word)
? parsedText[word]++
: parsedText[word] = 1
})
_.forIn(parsedText, (value, key) => {
if (value < 3) return
const word = {
word: key,
count: value
}
repetitiveWords.push(word)
})
return _.orderBy(repetitiveWords, ['count'], ['desc'])
}