-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (28 loc) · 1010 Bytes
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const path = require('path');
const fetchDom = require('./scripts/fetchDom');
const extractData = require('./scripts/extractData');
const locateTree = require('./scripts/locateTree');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
app.post('/', async (req, res) => {
const url = req.body.url;
try {
const domBody = await fetchDom(url);
const extractedBody = extractData(domBody);
const locatedTree = locateTree(extractedBody);
res.send({ treeDom: locatedTree.node, canvasOffsets: locatedTree.canvasOffsets });
} catch (err) {
res.status(500).send({ error: err.message });
}
});
const port = process.env.PORT || 3001;
app.listen(port, () => {
console.log('App listening at port:', port);
});