This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
forked from adrianbarwicki/vicigo
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
228 lines (179 loc) · 5.79 KB
/
app.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require('dotenv').config();
const express = require('express');
const app = express();
const cors = require('cors');
const flash = require('connect-flash');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const expressLayouts = require('express-ejs-layouts');
const compression = require('compression');
const seo = require('./server/seo');
const axios = require("axios");
const port = process.env.PORT;
/**
const showdown = require("showdown");
const converter = new showdown.Converter();
const convertMarkdownToHtml = (str) => converter.makeHtml(str);
*/
const preparePost = (post) => {
const nextPost = { ...post };
/**
nextPost.body = convertMarkdownToHtml(nextPost.bodyMD);
if (nextPost.userPosts && nextPost.userPosts.length) {
nextPost.userPosts = nextPost.userPosts.map(preparePost);
}
*/
return nextPost;
};
const isBot = (userAgent, push) => {
userAgent = userAgent ? userAgent.toLowerCase() : "unknown user-agent";
if (userAgent.indexOf("facebook") > -1 ||
userAgent.indexOf("twitter") > -1 ||
userAgent.indexOf("reddit") > -1 ||
userAgent.indexOf("google") > -1 ||
userAgent.indexOf("bing") > -1 ||
userAgent.indexOf("bot") > -1 ||
userAgent.indexOf("spider") > -1 ||
userAgent.indexOf("quora") > -1 ||
push == "true") {
return true;
}
return false;
};
app.get('/*', function(req, res, next) {
if (!req.headers.host) {
next();
} else {
if (req.headers.host.match(/^www/) !== null ) {
res.redirect('https://' + req.headers.host.replace(/^www\./, '') + req.url);
} else {
next();
}
}
});
app.use(compression({ filter: function(req,res) { return true; }} ));
app.use(cors({}));
app.use((req, res, next) => {
res._uglifyMangle = true;
next();
});
app.set('layout', 'bin');
//app.locals.pretty = false;
app.set('json spaces', 4);
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(expressLayouts);
app.use(cookieParser());
app.use(bodyParser());
app.use(flash());
app.use("/js", express.static(__dirname + "/public/js"));
app.use("/img", express.static(__dirname + "/public/img"));
app.use("/css", express.static(__dirname + "/public/css"));
app.use("/templates", express.static(__dirname + "/public/templates"));
app.use("/lib", express.static(__dirname + "/public/lib"));
app.use("/assets", express.static(__dirname + "/public/assets"));
app.use('/libs/', express.static(__dirname + '/node_modules'));
app.use('/libs', express.static(__dirname + '/node_modules'));
app.use("/", express.static(__dirname + "/public/"));
app.get("/91", (_, res) => res.redirect(301, '/dagur/lets-talk-spec-3-remove-the-transaction-undersize-limit-91'));
const renderFeed = async (req, res, next) => {
const userAgent = req.headers['user-agent'];
const crawlerView = req.query.crawlerView;
if (!isBot(userAgent, crawlerView)) {
return next();
}
let urlQuery = '';
if (req.params.hashtag) {
urlQuery += "?hashtag=" + req.params.hashtag;
}
const url = `https://honest.cash/api/posts${urlQuery}`;
const response = await axios.get(url);
const feeds = response.data;
const seoData = seo.metaDefault;
if (!feeds) {
res.status(404).render("Not found");
return;
}
res.render('feedBody.ejs', {
layout: 'crawlerFeedLayout',
SEO: seoData,
feeds
});
}
/**
* Server rendering for feed
*/
app.get("/", async (req, res, next) => {
const userAgent = req.headers['user-agent'];
const crawlerView = req.query.crawlerView;
if (!isBot(userAgent, crawlerView)) {
return next();
}
const hashtags = (await axios.get(`https://honest.cash/api/hashtag?limit=50`)).data;
res.render('index.ejs', {
SEO: seo.metaDefault,
layout: 'crawlerLayout',
hashtags
});
});
app.get("/top", renderFeed);
app.get("/new", renderFeed);
app.get("/hashtag/:hashtag", renderFeed);
/**
* Server rendering for profiles
*/
app.get("/profile/:username", async (req, res, next) => {
const userAgent = req.headers['user-agent'];
const crawlerView = req.query.crawlerView;
if (!isBot(userAgent, crawlerView)) {
return next();
}
let profileRes;
let feedRes;
let profile;
let feeds;
try {
profileRes = await axios.get(`https://honest.cash/api/user/${req.params.username}`);
profile = profileRes.data;
if (!profile) {
res.status(404).render("Not found");
return;
}
feedRes = await axios.get(`https://honest.cash/api/posts?userId=${profile.id}`);
feeds = feedRes.data.map(preparePost);
} catch (err) {
console.error(err);
return res.status(500).send("Internal Server Error");
}
const seoData = seo.getForProfile(profile);
res.render('profileBody.ejs', {
layout: 'crawlerProfileLayout',
SEO: seoData,
profile,
feeds
});
});
// V1
app.get("/profile/:username", (_, res) => res.sendfile("app.html", { root: __dirname + "/public" }));
// PATHS MIGRATED TO V2
app.get("/:username/:postId", (req, res) => res.redirect(`/v2/${req.params.username}/${req.params.postId}`));
app.get("/post/:username/:postId", (req, res) => res.redirect(`/v2/post/${req.params.username}/${req.params.postId}`));
app.get("/post/:postId", (req, res) => res.redirect(`/v2/post/${req.params.postId}`));
app.get("/write", (_, res) => res.redirect(`/v2/editor/write`));
app.get("/edit/:postId", (req, res) => res.redirect(`/v2/editor/edit/${req.params.postId}`));
app.get("/write/response/:parentPostId", (req, res) => res.redirect(`/v2/editor/comment/${req.params.parentPostId}`));
for (let v2Path of [
"/login", "/signup", "/thank-you", "/about"
]) {
app.get(v2Path, (_, res) =>
res.redirect(`/v2${v2Path}`)
);
}
// Welcome paths
app.get("/*", (_, res) => res.sendfile("app.html", { root: __dirname + "/public" }));
var server = require('http').Server(app);
server.listen(port, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Honest.Cash app listening at http://%s:%s', host, port);
});