forked from echoprotocol/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (48 loc) · 1.7 KB
/
server.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
const express = require('express');
const config = require('config');
const axios = require('axios');
const app = express();
const fs = require('fs');
const accountRegex = RegExp(/\/accounts\/.*\/info/);
const contractRegex = RegExp(/\/contracts\/.*\/info/);
const TIMEOUT_FETCH = 3000;
const filePath = `${__dirname}/dist/index.html`;
let fileIndex = null;
app.use(express.static(`${__dirname}/dist/`, { index: false }));
app.get('*', async (req, res) => {
let result = fileIndex;
let title = '';
let description = '';
let image = '';
const { url } = req;
if (accountRegex.test(url)) {
const accountName = url.split('/')[2];
title = `Account ${accountName} | Echo Explorer`;
description = 'ECHO account page';
image = `${config.SERVER_URL}/api/accounts/${accountName}/avatar.png`;
} else if (contractRegex.test(url)) {
const contractId = url.split('/')[2];
try {
const contract = await axios.get(`${config.SERVER_URL}/api/contracts/${contractId}`, { timeout: TIMEOUT_FETCH });
if (contract) {
title = `Contract ${contract.name || contractId} | Echo Explorer`;
description = contract.description || 'ECHO contract page';
image = `${config.SERVER_URL}${contract.icon}`;
}
} catch (err) {
console.warn('Error: ', err.code);
}
}
if (title) {
const metaTags = `\n<meta property="og:title" content=${title} />
<meta property="og:description" content=${description} />
<meta property="og:image" content=${image} />\n`;
const indexInsert = result.indexOf('</head>');
result = result.slice(0, indexInsert) + metaTags + result.slice(indexInsert);
}
res.send(result);
});
fs.readFile(filePath, 'utf8', async (error, data) => {
fileIndex = data;
app.listen(3000);
});