-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapp.js
45 lines (39 loc) · 1.09 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
const PORT = 8000;
const express = require("express");
const app = express();
const cors = require("cors");
app.use(cors());
const fs = require("fs");
const path = require("path");
const scrapeDynamicWebpage = require("./scrapers/dynamincSiteScraper");
const scrapeStaticWebpage = require("./scrapers/staticSiteScraper");
var folder = "./data";
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
console.log("Folder Created Successfully.");
}
app.get("/", function (req, res) {
res.send("Happy web scraping");
});
app.get("/api/v1/items", function (req, res) {
fs.readdir("./data", (err, files) => {
if (err) {
console.error(err);
res.status(500).send(err);
} else {
let data = [];
files.forEach((file) => {
const filePath = path.join("./data", file);
const fileData = JSON.parse(fs.readFileSync(filePath, "utf8"));
data = data.concat(fileData);
});
res.json(data);
}
});
});
app.listen(PORT, () => {
console.log("Server running on port " + PORT);
});
// scrapeDynamicWebpage();
scrapeStaticWebpage();
scrapeDynamicWebpage();