-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (75 loc) · 2.61 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
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
const {readFile} = require('fs').promises;
const express = require('express');
const app = express();
const port = 3000;
//Used to fetch external site pages.
const fetch = require('node-fetch');
//File location for sendFile
//Files need to be in a separate folder- they cannot be in the root directory, or strange behavior with URL forwarding happens.
const page_root = __dirname + '/pages/'
//Making it so that page file extensions (html) don't show up in the URL
app.use(express.static('.', {
extensions: ['html', 'htm']
}));
//Formerly used this so that youtube videoIDs retained case sensitivity when using them for routing.
//Not using this anymore because I am not routing based on the videoID anymore.
//app.set('case sensitive routing', true);
var pageVisitCount = 0;
//The home page.
app.get('/', async (request, response) => {
//console.log("HOME PAGE");
//Counting the number of visits
pageVisitCount++;
console.log(`Visit count: ${pageVisitCount}`);
//Show the main page.
response.sendFile('home.html', {root: page_root});
//readFile does not work here. If used, the page hangs indefinitely.
});
//Disabled the watch page because I'd rather people watch the video on YouTube.
/*
//The watch page.
app.get('/watch', async (request, response) => {
//console.log("WATCH PAGE")
let videoID = request.query.v;
//No search term given - redirect to main page.
if(videoID == null) {
response.redirect(307, '/')
}
//Load the page.
else {
response.sendFile('view_video.html', {root: page_root});
}
});
*/
//Allows client to read pages from other sites without any CORS errors.
app.get('/scrape', async (request, response) => {
let URL = request.query.url;
//No URL given - redirect to main page.
if(URL == null) {
response.redirect(307, '/')
}
//Load the given page.
else {
fetch(URL)
.then(response => response.text())
.then(data => {
response.send(data);
})
.catch(err => {
response.send(err);
});
}
});
//testing example
app.get('/example', async (request, response) => {
response.sendFile('example.html', {root: page_root});
})
//Catch all other URLs and redirect to main page.
app.get('*', async (request, response) => {
//console.log("CATCH-ALL REDIRECT")
//Need to subtract visit count by 1 or else it procs the pageVisitCount++ in the app.get('/')
pageVisitCount--;
//Redirect to the main page.
response.redirect(307, '/')
})
app.listen(process.env.PORT || port, () => console.log(`App available on http://localhost:${port}`));