-
Notifications
You must be signed in to change notification settings - Fork 1
/
server-fake-site.js
96 lines (75 loc) · 2.8 KB
/
server-fake-site.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
require('dotenv').config()
const cors = require('cors')
const express = require('express');
const bodyParser = require('body-parser');
// Constants
const SITE_PORT = process.env.SITE_PORT || 2000;
const SITE_HOST = process.env.SITE_HOST || '0.0.0.0';
// App
const app = express();
app.use(cors())
app.use(bodyParser.json())
const nunjucks = require('nunjucks');
nunjucks.configure('views', {
autoescape: false,
express: app
});
// START Templating code just for POC
const slashes = require("connect-slashes");
function createHash(str) {
var hash = 0;
if (str.length == 0) return hash;
for (var i = 0; i < str.length; i++) {
var char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
app.get(['/', '/perspective-api-test'], slashes(), function (req, res) {
let protocol = process.env.PROTOCOL || 'https://'
let pageSlug = req.originalUrl
let coralAuthorId = req.query.caid
if (coralAuthorId) {
// remove query params
pageSlug = req.originalUrl.split('?')[0]
}
let pageSlugHash = createHash(pageSlug);
const pageFullUrl = protocol + req.get('host') + pageSlug;
pageSlugHash = pageSlugHash < 0 ? pageSlugHash * -1 : pageSlugHash;
let opts = {
tabTitle: `${pageSlug} - od`,
pageSlugHash: pageSlugHash,
pageTitle: `An oD article with page slug ${pageSlug}`,
pageFullUrl: pageFullUrl.includes('https://localhost') ? process.env.PAGE_ROOT_URL + pageSlug : pageFullUrl,
serviceRootUrl: process.env.SERVICE_ROOT_URL,
coralRootUrl: process.env.CORAL_ROOT_URL,
coralAuthorId: coralAuthorId ? coralAuthorId : ''
}
res.render('coral-test.html', opts);
});
app.get('/articles/*', slashes(), function (req, res) {
let protocol = process.env.PROTOCOL || 'https://'
let pageSlug = req.originalUrl
let coralAuthorId = req.query.caid
if (coralAuthorId) {
// remove query params
pageSlug = req.originalUrl.split('?')[0]
}
let pageSlugHash = createHash(pageSlug);
const pageFullUrl = protocol + req.get('host') + pageSlug;
pageSlugHash = pageSlugHash < 0 ? pageSlugHash * -1 : pageSlugHash;
let opts = {
tabTitle: `${pageSlug} - od`,
pageSlugHash: pageSlugHash,
pageTitle: `An oD article with page slug ${pageSlug}`,
pageFullUrl: pageFullUrl.includes('https://localhost') ? process.env.PAGE_ROOT_URL + pageSlug : pageFullUrl,
serviceRootUrl: process.env.SERVICE_ROOT_URL,
coralRootUrl: process.env.CORAL_ROOT_URL,
coralAuthorId: coralAuthorId ? coralAuthorId : ''
}
res.render('coral.html', opts);
});
app.listen(SITE_PORT, SITE_HOST);
console.log(`Running on http://${SITE_HOST}:${SITE_PORT}`);