-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
49 lines (33 loc) · 1.13 KB
/
app.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
'use strict';
const express = require('express');
const { Trie } = require('./bll/trie');
const { PerformanceLogger } = require('./bll/performanceLogger');
const dataManager = require('./bll/dataManager');
const app = express();
const port = process.env.PORT || 5555;
// Create an empty Trie
const trie = new Trie();
app.get('/api/words-counter', (req, res) => {
try {
const input = req.query.input;
dataManager.processInput(input, trie);
// Here we could sent a json status info that say that it could take some minutes to process the text if is a big file
res.sendStatus(200);
}
catch (ex) {
res.sendStatus(500);
}
});
app.get('/api/words-statistics', (req, res) => {
try {
const word = req.query.word.trim().toLowerCase();
const perfTimer = new PerformanceLogger(word);
const wordCount = trie.getWordCount(word);
perfTimer.printWordLookupTime();
res.send({word: word, count: wordCount === -1 ? 0 : wordCount});
}
catch (ex) {
res.sendStatus(500)
}
});
app.listen(port, () => console.log(`Listening on port ${port}`));