paritybit.ca

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 5dc46d61add7bfa6716423653a8a8152c7fc93e0
parent d8fb8dc6f4692537469085a6155edd0298259a54
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Fri,  9 Aug 2019 23:15:53 -0400

Delete server/app.js since migrating to NGINX

Diffstat:
Dserver/app.js | 183-------------------------------------------------------------------------------
1 file changed, 0 insertions(+), 183 deletions(-)

diff --git a/server/app.js b/server/app.js @@ -1,183 +0,0 @@ -"use strict"; -/* @file app.js - * @author Jake Bauer - * @date 2019-03-30 - * @original-date 2018-12-07 - * - * @brief The code for the web server. Handles all HTTP/1.1 methods. - * Nginx handles compression and redirecting to HTTPS. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. - */ - -let fs = require("fs"); -let http = require("http"); -let path = require("path"); -let crypto = require("crypto"); - -let pageHits = {}; - -function read_page_hits() { - fs.readFileSync("./public/share/pageHits.json", (err, content) => { - if (err) { - console.log("No existing hitcount data. Creating new file."); - } - else { - console.log("Reading in existing page hit data."); - pageHits = JSON.parse(content); - } - }); -} - -function save_page_hits() { - fs.writeFile("./public/share/pageHits.json", JSON.stringify(pageHits), err => { - if (err) { - return console.log("Page Hit Write Error: " + err); - } - }); -} - -function handle_error(err, res) { - // If the file cannot be found on the server - if (err.code === "ENOENT") { - fs.readFile("./public/html/404.html", (err, content) => { - res.writeHead(404, {"Content-Type": "text/html"}); - res.end(content, "utf-8"); - }); - } - else { - // If there is any other error, report error 500 - console.log(err); - res.writeHead(500, {"Content-Type": "text/plain"}); - res.end("HTTP Server Error: " + err.code + "\n"); - } -} - -function compute_etag(content) { - let hash = crypto.createHash("sha1"); - hash.setEncoding("hex"); - hash.write(content); - hash.end(); - return hash.read(); -} - -function serve_regular_file(filePath, contentType, req, res) { - fs.readFile(filePath, (err, content) => { - let etag = compute_etag(content); - // If the resource is cached, send code 304 and don't send resource - if (etag === req.headers["if-none-match"]) { - res.writeHead(304, {"Content-Type": contentType, - "Cache-Control": "max-age=120", "ETag": etag }); - res.end(); - } - else { - // Otherwise, send the file - res.writeHead(200, {"Content-Type": contentType, - "Cache-Control": "max-age=120", "ETag": etag}); - res.end(content, "utf-8"); - } - }); -} - -function serve_large_file(filePath, fileSize, contentType, req, res) { - // The size of the file is sent as the ETag to prevent the server from - // having to do expensive hashing of large files. - let etag = fileSize; - if (etag === req.headers["if-none-match"]) { - res.writeHead(304, {"Content-Type": contentType, - "Content-Length": fileSize, "ETag": etag, - "Cache-Control": "max-age=120"}); - res.end(); - } - else { - // Otherwise, send the file - res.writeHead(200, {"Content-Type": contentType, - "Content-Length": fileSize, "ETag": etag, - "Cache-Control": "max-age=120"}); - let readStream = fs.createReadStream(filePath); - readStream.on("open", () => { - readStream.pipe(res); - }); - } -} - -const httpServer = http.createServer((req, res) => { - let mimeTypes = { - '.html': 'text/html', - '.js': 'text/javascript', - '.css': 'text/css', - '.txt': 'text/plain', - '.xml': 'application/rss+xml', - '.json': 'application/json', - '.svg': 'application/image/svg+xml', - '.png': 'image/png', - '.jpg': 'image/jpg', - '.gif': 'image/gif', - '.ico': 'image/x-icon', - '.mp3': 'audio/mpeg', - '.mp4': 'video/mp4', - '.mkv': 'video/x-matroska' - } - - let filePath = "./public" + req.url; - // Serve the default page if none specified - if (req.url === "/") { - req.url = "/home.html"; - } - - // Get the extension of the file requested and therefore the content type - let extName = String(path.extname(req.url)).toLowerCase(); - // If url does not specify a file extension then assume html file requested - if (extName === "") { - extName = ".html"; - req.url += ".html"; - } - let contentType = mimeTypes[extName] || "application/octet-stream"; - // Make contentType exception for sitemap.xml file (everything else is RSS) - if (req.url === "/sitemap.xml") { - contentType = "application/xml"; - } - - // Append html directory for serving pages (other resources will be - // addressed directly as (e.g.) "/css/base.min.css") - if (extName === ".html") { - filePath = "./public/html" + req.url; - } - - // Count page hits - if (pageHits[filePath] >= 1) { - pageHits[filePath] += 1; - } - else { - pageHits[filePath] = 1; - } - - fs.stat(filePath, (err, stat) => { - if (err) { - handle_error(err, res); - } - else { - if (stat.size > 20000) { - serve_large_file(filePath, stat.size, contentType, req, res); - } - else { - serve_regular_file(filePath, contentType, req, res); - } - } - }); -}).listen(process.env.NODE_PORT || 8080); - -read_page_hits(); -setInterval(save_page_hits, 2000); -console.log("Server running.");