bulet123 commited on
Commit
e36203b
·
verified ·
1 Parent(s): 130630b

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +45 -0
server.js ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const axios = require('axios');
3
+ const cheerio = require('cheerio');
4
+ const cors = require('cors');
5
+ const bodyParser = require('body-parser');
6
+
7
+ const app = express();
8
+ app.use(cors());
9
+ app.use(bodyParser.json());
10
+
11
+ // Scraping endpoint
12
+ app.post('/api/scrape', async (req, res) => {
13
+ try {
14
+ const { url } = req.body;
15
+
16
+ const response = await axios.get(url, {
17
+ headers: {
18
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
19
+ }
20
+ });
21
+
22
+ const $ = cheerio.load(response.data);
23
+
24
+ const data = {
25
+ url,
26
+ content: $('body').text().substring(0, 5000), // Limit content
27
+ pageCount: 1,
28
+ imageCount: $('img').length,
29
+ linkCount: $('a').length,
30
+ timestamp: new Date().toISOString()
31
+ };
32
+
33
+ res.json(data);
34
+ } catch (error) {
35
+ res.status(500).json({
36
+ error: 'Scraping failed',
37
+ details: error.message
38
+ });
39
+ }
40
+ });
41
+
42
+ const PORT = process.env.PORT || 3001;
43
+ app.listen(PORT, () => {
44
+ console.log(`Server running on port ${PORT}`);
45
+ });