paritybit.ca

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

commit cea91c5e6f326e87e8073428e1ea525397f167ce
parent e1944f19525900471a7df19607b99b2cc5a53213
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Tue, 26 Mar 2019 23:46:28 -0400

Clean up backend server code

Remove gzip-related functionality as this is handled by nginx. Also
add new comments and re-write old ones to be clearer.

Diffstat:
Mserver/app.js | 58+++++++++++++++++++++++++---------------------------------
1 file changed, 25 insertions(+), 33 deletions(-)

diff --git a/server/app.js b/server/app.js @@ -1,10 +1,11 @@ "use strict"; /* @file app.js * @author Jake Bauer - * @date 2019-02-28 + * @date 2019-03-26 * @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 @@ -23,10 +24,9 @@ let fs = require("fs"); let http = require("http"); let path = require("path"); -let zlib = require("zlib"); let crypto = require("crypto"); -function file_read_error(err, res) { +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) => { @@ -73,6 +73,7 @@ const httpServer = http.createServer((req, res) => { req.url = "/home.html"; } + // Get the extension of the file requested and therefore the content type extName = String(path.extname(req.url)).toLowerCase(); contentType = mimeTypes[extName] || "application/octet-stream"; @@ -82,39 +83,30 @@ const httpServer = http.createServer((req, res) => { filePath = "./public/html" + req.url; } - // Compression enabled - acceptEncoding = String(req.headers["accept-encoding"]).split(','); - if (false && acceptEncoding.contains("gzip")) { - const gzip = zlib.createGzip(); - const input = fs.createReadStream(); - const output = fs.createWriteStream(); - input.pipe(gzip).pipe(output); - } - else { - fs.readFile(filePath, (err, content) => { - if (err) { - file_read_error(err, res); + fs.readFile(filePath, (err, content) => { + if (err) { + handle_error(err, res); + } + else { + 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, + "Strict-Transport-Security": "max-age=604800;" + + " includeSubDomains", "Cache-Control": "max-age=120", + "ETag": etag }); + res.end(); } else { - let etag = compute_etag(content); - if (etag === req.headers["if-none-match"]) { - res.writeHead(304, {"Content-Type": contentType, - "Strict-Transport-Security": "max-age=604800;" - + " includeSubDomains", "Cache-Control": "max-age=120", - "ETag": etag }); - res.end(); - } - else { - // Otherwise, add header and footer and send the file - res.writeHead(200, {"Content-Type": contentType, - "Strict-Transport-Security": "max-age=604800;" - +" includeSubDomains", "Cache-Control": "max-age=120", - "ETag": etag}); - res.end(content, "utf-8"); - } + // Otherwise, send the file + res.writeHead(200, {"Content-Type": contentType, + "Strict-Transport-Security": "max-age=604800;" + +" includeSubDomains", "Cache-Control": "max-age=120", + "ETag": etag}); + res.end(content, "utf-8"); } - }); - } + } + }); }).listen(8080); console.log("Server running.");