-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (30 loc) · 882 Bytes
/
index.js
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
const PORT = 8000;
const axios = require('axios');
const cheerio = require('cheerio');
const express = require('express');
const app = express();
const cors = require('cors');
app.use(cors());
const url = 'https://www.theguardian.com/uk'
app.get('/', (req, res) => {
res.json('This is my Web Scraper');
})
app.get('/results', (req, res) => {
axios(url)
.then(response => {
const html = response.data;
const $ = cheerio.load(html);
const articles = [];
$('.fc-item__title', html).each(() => { // <-- Cannot be a function expression
const title = $(this).text();
const url = $(this).find('a').attr('href');
articles.push({
title,
url
});
});
res.json(articles);
})
.catch(err => console.log(err));
})
app.listen(PORT, () => console.log(`server runnnig on PORT ${PORT}`));