-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (39 loc) · 1.48 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const axios = require('axios');
const cheerio = require('cheerio');
const cors = require('cors');
const dotenv = require('dotenv');
const express = require('express');
dotenv.config();
const app = express();
app.use(cors());
const pattern = /^http(s)?:\/\/?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/gm;
app.get('/', (req, res) => {
if (!req.query.url) res.status(200).json('Default message');
const { url } = req.query;
if (pattern.test(url)) {
axios(url)
.then(response => {
const html = response.data;
const elements = cheerio.load(html);
const getTypeTag = (type) => {
return elements(`meta[name="${type}"]`).attr('content') || elements(`meta[property="${type}"]`).attr('content')
}
const getDataFrom = (nameTag) => {
return getTypeTag(nameTag) || getTypeTag(`og:${nameTag}`) || getTypeTag(`twitter:${nameTag}`);
}
const getMetadata = () => ({
title: elements('title').text() || getDataFrom('title'),
description: getDataFrom('description'),
image: getDataFrom('image'),
url: getDataFrom('url')
});
const scrapedData = getMetadata();
res.status(200).json(scrapedData);
}).catch(error => {
return res.status(404).jsonp({ error: error })
})
} else {
res.status(404).jsonp({ error: 'URL not valid' })
}
})
app.listen(process.env.PORT, () => console.log(`Server running on port: ${process.env.PORT}`));