-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
39 lines (31 loc) · 1.16 KB
/
server.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
const express = require("express");
const path = require("path");
const app = express();
const weblinkScraper = require('./js/weblinkScraper');
app.use(express.json());
app.listen(8080, () => {
console.log("Server is listening on port 8080");
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + "/index.html"));
});
// Serve the results page
app.get('/results', (req, res) => {
res.sendFile(path.join(__dirname + "/results.html"));
});
// Handle the data from the client to perform web scraping
app.post("/info", async (req, res) => {
const { parcel } = req.body;
if (!parcel) {
return res.status(400).send({ status: "fail: empty parcel body" });
}
try {
const results = await weblinkScraper(parcel);
// Store the results in a session or a temporary store
// For simplicity, we'll pass the results as a query parameter (not recommended for large data)
res.redirect(`/results?data=${encodeURIComponent(JSON.stringify(results))}`);
} catch (error) {
console.error('Error during web scraping:', error);
res.status(500).send({ status: "fail", error: error.message });
}
});