-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (63 loc) · 2.85 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express');
const path = require('path');
const fetch = require('node-fetch');
const isbot = require('isbot');
const app = express();
const baseApiUrl = "https://api.tvmaze.com"; // Taken from 'http://www.tvmaze.com/api' (No token/auth required to use this API)
const port = process.env.PORT || 9999;
const appPath = 'dist'; // The path to your build folder that contains your SPA
const defaultTitle = 'Default title for your SPA';
const defaultDescription = 'Default description for your SPA';
const defaultImage = path.join(__dirname, `${appPath}/favicon.ico`); // Your SPA's favicon
function metaTags(title, description, image) {
return `
<html>
<head>
<title>${defaultTitle}</title>
<meta charset="utf-8">
<link rel="icon" href="${defaultImage}" />
<meta class="meta-description" name="description" content="${defaultDescription}">
<!-- Google+ / Schema.org -->
<meta itemprop="name" content="${title}">
<meta itemprop="description" content="${description}">
<meta itemprop="image" content="${image}">
<!-- Twitter Meta -->
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@TV Maze" />
<meta name="twitter:title" content="${title}" />
<meta name="twitter:description" content="${description}" />
<meta name="twitter:image" content="${image}"/>
<!-- Facebook Meta -->
<meta property="og:type" content="article" />
<meta property="og:title" content="${title}" />
<meta property="og:description" content="${description}" />
<meta property="og:image" content="${image}" />
</head>
<body>
</body>
</html>`;
}
app.use(express.static(path.join(__dirname, appPath)));
app.use((req, res, next) => {
const userAgent = req.headers['user-agent'] || ''; // Detects if the request came from a browser or a crawler bot.
if (isbot(userAgent)) next();
else res.sendFile(path.join(__dirname, `${appPath}/index.html`));
});
app.get('/tvshow/:showId', (req, res) => { // The url path '/tvshow/:id' should match the one in your SPA
const {showId} = req.params
fetch(`${baseApiUrl}/shows/${showId}`)
.then(response=> response.json())
.then((data) => {
// Fill the meta title, description and image you would want to show when sharing the link '/tvshows'
const {name, image, status} = data;
if (status === 404) res.send(metaTags(defaultTitle, defaultDescription, defaultImage));
else res.send(metaTags(name, name, image.medium));
})
.catch((error) => {
res.send(metaTags(defaultTitle, defaultDescription, defaultImage));
});
});
app.get('*', (req, res) => { // Any other url of your SPA that doesn't need any SEO
res.send(metaTags(defaultTitle, defaultDescription, defaultImage));
});
app.listen(port, () => { console.log(`Node running on port ${port}`) });