-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
69 lines (53 loc) · 1.89 KB
/
server.js
File metadata and controls
69 lines (53 loc) · 1.89 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import express from 'express';
import fs from 'fs';
import path from 'path';
import PDFDocument from 'pdfkit';
import scrape from './scraper/puppeteer.js';
import {connectDB} from './mongodb.js';
const app = express();
const port = 3000;
connectDB();
app.use('/downloads', express.static(path.join(process.cwd(),'downloads')));
app.use(express.static(path.resolve('.')))
app.get('/', (req, res) => {
res.send("Server running, Yay!!")
})
app.get('/scrape', async (req, res) => {
const url = req.query.url;
if(!url || !url.startsWith('http')) {
return res.status(500).json({error: "Invalid or missing url"});
}
try {
const data = await scrape(url);
const fileName = `scraped_${Date.now()}.pdf`;
const filePath = path.join('downloads', fileName);
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream(filePath));
doc.fontSize(22).text(data.title || "No Title", {underline: true});
doc.moveDown();
doc.fontSize(14).fillColor('black').text(`Date: ${data.authorDate || new Date().toISOString()}`);
doc.moveDown();
doc.fontSize(14).fillColor('black').text(`URL: `, {continued: true});
doc.fillColor('blue').text(url);
doc.moveDown();
doc.fillColor('black');
data.content.forEach(paragraph => {
doc.text(paragraph,{
wordSpacing: 3,
lineGap: 6,
});
doc.moveDown();
});
doc.end();
res.json({
message: "Scraping done and data saved!",
downloadLink: `${req.protocol}://${req.get('host')}/downloads/${fileName}`
})
}catch(err){
console.log("Scraping failed: ", err);
res.status(500).json({error: "Something went wrong during scraping"})
}
});
app.listen(port, () => {
console.log(`App running on http://localhost:${port}`)
})