-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
46 lines (32 loc) · 1.08 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
import express from 'express';
import validateUrl from './features/validateUrl.js';
import scrapeWeb from './features/scrapeWeb.js';
import getRelevantWords from './features/getRelevantWords.js';
import updateCloud from './features/updateCloud.js';
const api = express();
api.use(express.json());
const cloud = {
wordDetails: [],
totalWordOccurrences: 0
}
const fetchedURLs = []
api.post('/', async (req, res, next) => {
try {
const productUrl = req.query.productUrl;
// avoid fetching repeatedly
validateUrl(productUrl, fetchedURLs);
const productDescription = await scrapeWeb(productUrl);
const relevantWords = getRelevantWords(productDescription);
// include new relevant words to 'cloud'
updateCloud(relevantWords, cloud);
res.json(cloud);
} catch (error) {
next(error)
}
});
api.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({ stack: err.stack || '' });
});
const port = 3000;
api.listen(port, () => console.log(`API running at localhost:${port}`));