-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (35 loc) · 1.17 KB
/
Copy pathserver.js
File metadata and controls
45 lines (35 loc) · 1.17 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
// Este ejemplo de Template busca Webcams en la red
import express from 'express';
import fetch from 'node-fetch';
import * as cheerio from 'cheerio';
const app = express();
const PORT = process.env.PORT || 3000;
// Ruta de bienvenida
app.get('/', (req, res) => {
res.send('<h1>Servidor Node.js + Express funcionando 🚀</h1>');
});
// Ruta ejemplo de scraping
app.get('/camaras', async (req, res) => {
try {
const url = 'https://www.example.com'; // Cambia esto por la página que quieras scrapear
const response = await fetch(url);
const body = await response.text();
const $ = cheerio.load(body);
const camaras = [];
$('img').each((i, elem) => {
const src = $(elem).attr('src');
if (src && src.includes('cam')) { // Ajusta este filtro a lo que quieras encontrar
camaras.push({
imagen: src.startsWith('http') ? src : `${url}${src}`
});
}
});
res.json(camaras);
} catch (error) {
console.error('Error capturando cámaras:', error);
res.status(500).json({ error: 'Error capturando cámaras' });
}
});
app.listen(PORT, () => {
console.log(`Servidor arrancado en http://localhost:${PORT} 🚀`);
});