scraper-1-1 / server.js
bulet123's picture
Create server.js
e36203b verified
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
app.use(cors());
app.use(bodyParser.json());
// Scraping endpoint
app.post('/api/scrape', async (req, res) => {
try {
const { url } = req.body;
const response = await axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
});
const $ = cheerio.load(response.data);
const data = {
url,
content: $('body').text().substring(0, 5000), // Limit content
pageCount: 1,
imageCount: $('img').length,
linkCount: $('a').length,
timestamp: new Date().toISOString()
};
res.json(data);
} catch (error) {
res.status(500).json({
error: 'Scraping failed',
details: error.message
});
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});