const express = require("express"); const app = express(); const { MongoClient } = require("mongodb"); const cors = require("cors"); const getData = require("./modules/getData.js"); const fs = require("fs"); const { port, URI, database, collection, screenshotDirectory, pdfDirectory, } = require("./config.js"); const fetch = require("cross-fetch"); const sanitize = require("sanitize-filename"); const client = new MongoClient(URI); const db = client.db(database); const list = db.collection(collection); // Create the storage directories if they do not exist if (!fs.existsSync(screenshotDirectory)) { fs.mkdirSync(screenshotDirectory, { recursive: true }); } if (!fs.existsSync(pdfDirectory)) { fs.mkdirSync(pdfDirectory, { recursive: true }); } app.use(cors()); app.use(express.json()); app.get("/api", async (req, res) => { const data = await getDoc(); res.send(data); }); app.get("/screenshots/:id", async (req, res) => { res.sendFile( __dirname + "/" + screenshotDirectory + "/" + sanitize(req.params.id), (err) => { if (err) { res.sendFile(__dirname + "/pages/404.html"); } } ); }); app.get("/pdfs/:id", async (req, res) => { res.sendFile( __dirname + "/" + pdfDirectory + "/" + sanitize(req.params.id), (err) => { if (err) { res.sendFile(__dirname + "/pages/404.html"); } } ); }); app.post("/api", async (req, res) => { const pageToVisit = req.body.link; const id = req.body._id; const getTitle = async (url) => { let body; await fetch(url) .then((res) => res.text()) .then((text) => (body = text)); // regular expression to parse contents of the