-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
74 lines (61 loc) · 2.08 KB
/
app.js
File metadata and controls
74 lines (61 loc) · 2.08 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require('express');
const bodyParser = require('body-parser');
const ejsMate = require('ejs-mate');
const path = require('path');
const cheerio = require('cheerio');
const axios = require('axios').default;
const app = express();
app.engine('ejs', ejsMate);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
let array = {}
let Image = ""
res.render('index', { array, Image })
});
app.get('/calc', (req, res) => {
res.render('calculator')
})
app.post('/', async (req, res) => {
try {
let www = req.body.url
let response, html, $, description, ogType;
let array = {};
try {
response = await axios.get(www)
} catch (e) {
response = await axios.get(`http://${www}`)
}
html = response.data;
array = [];
$ = cheerio.load(html);
function getMeta(name) {
let ans = $(`meta[property=og:${name}]`).attr('content')
return ans
}
let Image = getMeta("image") || $(`meta[name=image]`).attr('content') || "unfound.png"
array = {
URL: www,
Title: getMeta("title") || $(`meta[name=title]`).attr('content') || "NOT FOUND",
Type: getMeta("type") || $(`meta[name=type]`).attr('content') || "NOT FOUND",
Description: $(`meta[name=description]`).attr('content') || getMeta("description") || "NOT FOUND"
}
res.render('index', { array, Image })
}
catch (e) {
res.send(e)
}
})
const port = process.env.PORT || 3000
app.listen(port, (err) => {
if (err) console.log("Error in server setup")
console.log('Serving on port 3000')
});