-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
79 lines (66 loc) · 2.16 KB
/
app.js
File metadata and controls
79 lines (66 loc) · 2.16 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
75
76
77
78
79
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const cheerio = require('cheerio');
const fs = require('fs');
const fetch = require('cross-fetch');
const healthCheckHandler = require('./utils/healthCheck');
const { startMonitoring } = require('./utils/keepAlive');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(express.static('public'));
app.get('/health', healthCheckHandler);
app.get('/', (req, res) => {
res.render('index');
});
app.get('/topics-iframe', function (req, res) {
res.render('topics-iframe.ejs');
});
app.post('/articles/:slug', (req, res) => {
const slug = req.params.slug;
res.sendFile(__dirname + `/articles/${slug}.html`);
});
app.get('/articles/:slug', (req, res) => {
const slug = req.params.slug;
const fileContent = fs.readFileSync(
path.join(__dirname, 'views', 'index.ejs'),
'utf8'
);
const $ = cheerio.load(fileContent);
const basePath = 'https://dev.to/api/articles/js_bits_bill';
const url = `${basePath}/${slug}`;
fetch(url)
.then((res) => res.json())
.then((data) => {
$('meta[property="og:url"]').attr(
'content',
`https://jsbits-yo.com/articles/${data.slug}`
);
$('meta[property="og:type"]').attr('content', 'article');
$('meta[property="og:title"]').attr('content', data.title);
$('meta[property="og:image"]').attr('content', data.social_image);
$('meta[property="og:description"]').attr('content', data.description);
const modifiedFileContent = $.html();
res.send(modifiedFileContent);
})
.catch((err) => {
console.error(err);
res.render('index');
});
});
app.get('/linkedin-badge', (req, res) => {
res.render('linkedin-badge');
});
app.get('/bug-bash', (req, res) => {
res.status(301).redirect('https://www.udemy.com/course/js-bits-bug-bash');
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log('App running...');
if (process.env.NODE_ENV === 'production') {
const serverUrl =
process.env.RENDER_EXTERNAL_URL || `http://localhost:${PORT}`;
startMonitoring(serverUrl, 12);
}
});