Browse Source

refactored includes.js to deal with all static content for the website.

pull/4/head
jrtechs 6 years ago
parent
commit
8bfb0bfa1e
5 changed files with 141 additions and 132 deletions
  1. +24
    -18
      admin.js
  2. +7
    -0
      admin.sh
  3. +68
    -50
      includes/includes.js
  4. +0
    -35
      js/js.js
  5. +42
    -29
      server.js

+ 24
- 18
admin.js View File

@ -7,33 +7,41 @@
* @author Jeffery Russell 7-21-18 * @author Jeffery Russell 7-21-18
*/ */
//http server
const http = require('http'); const http = require('http');
//parsing request url
const url = require('url'); const url = require('url');
//express app
const express = require("express"); const express = require("express");
const app = express();
//session data for login
const session = require('express-session'); const session = require('express-session');
//sending static content
const includes = require('./includes/includes.js'); const includes = require('./includes/includes.js');
const app = express();
//used for file io
const utils = require('./utils/utils.js'); const utils = require('./utils/utils.js');
const port = 8001;
//cache -- only used for static contents
const cache = require('memory-cache'); const cache = require('memory-cache');
/** Initializes sessions for login */ /** Initializes sessions for login */
app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }})); app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
//port to listen for the admin server on
const port = 8001;
/** /**
* Parses the request url and calls correct JS files * Parses the request url and calls correct JS files
*/ */
app.use(function(request, res)
app.use(function(request, result)
{ {
//prevents people from pointing their dns at my IP:port for my site
if(request.headers.host.includes("localhost:" + port) || if(request.headers.host.includes("localhost:" + port) ||
request.headers.host.includes("jrtechs.net")) request.headers.host.includes("jrtechs.net"))
{ {
@ -42,28 +50,28 @@ app.use(function(request, res)
//handles image requests //handles image requests
if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png")) if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
{ {
require("./img/image.js").main(res, filename, cache);
includes.sendJS(result, filename, cache);
} }
else if(filename.includes("/css/") || filename.includes(".woff2")) else if(filename.includes("/css/") || filename.includes(".woff2"))
{ {
includes.sendCSS(res, filename, cache)
includes.sendCSS(result, filename, cache)
} }
else if(filename.includes("/js/") || filename.includes(".js")) else if(filename.includes("/js/") || filename.includes(".js"))
{ {
require("./js/js.js").main(res, filename, cache);
includes.sendJS(result, filename, cache);
} }
else else
{ {
res.writeHead(200, {'Content-Type': 'text/html'});
result.writeHead(200, {'Content-Type': 'text/html'});
file = "./admin/admin.js";
const file = "./admin/admin.js";
Promise.all([includes.printAdminHeader(), Promise.all([includes.printAdminHeader(),
require(file).main(filename, request), require(file).main(filename, request),
includes.printFooter()]).then(function(content) includes.printFooter()]).then(function(content)
{ {
res.write(content.join(''));
res.end();
result.write(content.join(''));
result.end();
}).catch(function(err) }).catch(function(err)
{ {
@ -74,12 +82,10 @@ app.use(function(request, res)
} }
else else
{ {
// utils.printWrongHost(res);
res.writeHead(418, {});
res.end();
// utils.printWrongHost(result);
result.writeHead(418, {});
result.end();
} }
}); });
http.createServer(app).listen(port);
http.createServer(app).listen(port);

+ 7
- 0
admin.sh View File

@ -0,0 +1,7 @@
#!/bin/bash
#
# Runs the admin nodejs server for jrtechs.net
#
# 8/18/18 Jeffery Russell
nodejs admin.js

+ 68
- 50
includes/includes.js View File

@ -1,17 +1,70 @@
/** /**
Includes.js
File used for getting the header and footer
* File: Includes.js
*
* Module used for fetching static content for the website
* like js, css, images, and other static html pages
*
* @author Jeffery Russell
*/ */
//used for file IO
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
//name of header file
const HEADER_FILE = "includes/header.html"; const HEADER_FILE = "includes/header.html";
//path of footer file
const FOOTER_FILE = "includes/footer.html"; const FOOTER_FILE = "includes/footer.html";
//admin header path
const ADMIN_HEADER = "includes/adminHeader.html"; const ADMIN_HEADER = "includes/adminHeader.html";
//used for hashing stuff for the header's e-tag for clients cache
const crypto = require('crypto'); const crypto = require('crypto');
/**
* Sends a static file to the client in a way which the web browser
* caches the contents sent.
*
* @param cache -- server's hashmap which reduces file io
* @param path -- file requested by user
* @param type -- type of file for the header
* @param result -- sent to client
*/
const sendCachedContent = function(cache, path, type, result)
{
const goods = cache.get(path);
if(goods == null)
{
utils.include("." + path).then(function(content)
{
const eTag = crypto.createHash('md5').update(content).digest('hex');
result.writeHead(200, {'Content-Type': type, 'Cache-Control':
'public, max-age=2678400', 'ETag': '"' + eTag + '"',
'Vary': 'Accept-Encoding'});
result.write(content);
result.end();
cache.put(path, content);
}).catch(function(error)
{
console.log(error);
});
}
else
{
const eTag = crypto.createHash('md5').update(goods).digest('hex');
result.writeHead(200, {'Content-Type': type,
'Cache-Control': 'public, max-age=2678400',
'ETag': '"' + eTag + '"',
'Vary': 'Accept-Encoding'});
result.write(goods);
result.end();
}
};
module.exports = module.exports =
{ {
/** Appends the header html section to the result which is /** Appends the header html section to the result which is
@ -55,34 +108,7 @@ module.exports =
*/ */
sendCSS: function(result, path, cache) sendCSS: function(result, path, cache)
{ {
const css = cache.get(path);
if(css == null)
{
utils.include("./" + path).then(function(content)
{
var eTag = crypto.createHash('md5').update(content).digest('hex');
result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control':
'public, max-age=2678400', 'ETag': '"' + eTag + '"',
'Vary': 'Accept-Encoding'});
result.write(content);
result.end();
cache.put(path, content);
}).catch(function(error)
{
console.log(error);
});
}
else
{
const eTag = crypto.createHash('md5').update(css).digest('hex');
result.writeHead(200, {'Content-Type': 'text/css',
'Cache-Control': 'public, max-age=2678400',
'ETag': '"' + eTag + '"',
'Vary': 'Accept-Encoding'});
result.write(css);
result.end();
}
sendCachedContent(cache, "/" + path, 'text/css', result);
}, },
@ -93,25 +119,17 @@ module.exports =
*/ */
sendImage: function(result, fileName, cache) sendImage: function(result, fileName, cache)
{ {
const img = cache.get(fileName);
if(img == null)
{
utils.include("." + fileName).then(function(content)
{
const eTag = crypto.createHash('md5').update(content).digest('hex');
console.log(eTag);
result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'});
result.write(content);
result.end();
cache.put(content);
});
}
else
{
const eTag = crypto.createHash('md5').update(img).digest('hex');
result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'});
result.write(img);
result.end();
}
sendCachedContent(cache, fileName, 'image/png', result);
},
/**Sends the user an image from the specified fileName.
*
* @param result
* @param fileName
*/
sendJS: function(result, fileName, cache)
{
sendCachedContent(cache, fileName, 'application/javascript', result);
} }
}; };

+ 0
- 35
js/js.js View File

@ -1,35 +0,0 @@
const utils = require('../utils/utils.js');
const crypto = require('crypto');
module.exports=
{
/**Sends the user an image from the specified fileName.
*
* @param result
* @param fileName
*/
main: function(result, fileName, cache)
{
//result.contentType = 'image/png';
var js = cache.get(fileName);
if(js == null)
{
utils.include("." + fileName).then(function(content)
{
var eTag = crypto.createHash('md5').update(content).digest('hex');
result.writeHead(200, {'Content-Type': 'application/javascript', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'});
result.write(content);
result.end();
cache.put(content);
});
}
else
{
var eTag = crypto.createHash('md5').update(js).digest('hex');
result.writeHead(200, {'Content-Type': 'application/javascript', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"', 'Vary': 'Accept-Encoding'});
result.write(js);
result.end();
}
}
};

+ 42
- 29
server.js View File

@ -5,72 +5,81 @@
* appropriate pages. * appropriate pages.
*/ */
//http server
const http = require('http'); const http = require('http');
//used to parse the request URL
const url = require('url'); const url = require('url');
//express app
const express = require("express"); const express = require("express");
//const session = require('express-session');
//express app
const app = express();
//used to append static content to result
const includes = require('./includes/includes.js'); const includes = require('./includes/includes.js');
//server side logging
const sql = require('./utils/sql'); const sql = require('./utils/sql');
//Used for gzip compression
const compression = require('compression')
//caching program to make the application run faster
const cache = require('memory-cache');
//Updates the site map whenever the server is started
const map = require('./utils/generateSiteMap.js'); const map = require('./utils/generateSiteMap.js');
map.main(); map.main();
const app = express();
//app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxAge: 6000000 }}));
//compresses the site
const compression = require('compression')
app.use(compression());
//port for the server to run on
const port = 8000; const port = 8000;
const cache = require('memory-cache');
/** /**
* Parses the request url and calls correct JS files * Parses the request url and calls correct JS files
*/ */
app.use(function(request, res)
app.use(function(request, result)
{ {
//prevents people from pointing their dns at my IP:port for my site
if(request.headers.host.includes("localhost:" + port) || if(request.headers.host.includes("localhost:" + port) ||
request.headers.host.includes("jrtechs.net")) request.headers.host.includes("jrtechs.net"))
{ {
const filename = url.parse(request.url, true).pathname; const filename = url.parse(request.url, true).pathname;
//handles image requests //handles image requests
if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png") || filename.includes(".ico"))
if(filename.includes("/img/") || filename.includes(".jpg") ||
filename.includes(".png") || filename.includes(".ico"))
{ {
includes.sendImage(res, filename, cache);
includes.sendImage(result, filename, cache);
} }
else if(filename.includes("/css/") || filename.includes(".woff2") || filename.includes(".txt"))
//css and font files
else if(filename.includes("/css/") || filename.includes(".woff2") ||
filename.includes(".txt"))
{ {
includes.sendCSS(res, filename, cache);
includes.sendCSS(result, filename, cache);
} }
//scripts
else if(filename.includes("/js/") || filename.includes(".js")) else if(filename.includes("/js/") || filename.includes(".js"))
{ {
require("./js/js.js").main(res, filename, cache);
includes.sendJS(result, filename, cache);
} }
//downloads
else if(filename.includes("/downloads/")) else if(filename.includes("/downloads/"))
{ {
require("./downloads/downloads.js").main(res, filename, request);
require("./downloads/downloads.js").main(result, filename, request);
} }
else else
{ {
var file = "";
const html = cache.get(filename); const html = cache.get(filename);
res.writeHead(200, {'Content-Type': 'text/html'});
result.writeHead(200, {'Content-Type': 'text/html'});
if(html == null) if(html == null)
{ {
var file = "";
if(filename === '' || filename === '/') if(filename === '' || filename === '/')
{ {
file="./posts/homePage.js"; file="./posts/homePage.js";
@ -90,8 +99,8 @@ app.use(function(request, res)
require(file).main(filename, request), require(file).main(filename, request),
includes.printFooter()]).then(function(content) includes.printFooter()]).then(function(content)
{ {
res.write(content.join(''));
res.end();
result.write(content.join(''));
result.end();
cache.put(filename, content.join('')); cache.put(filename, content.join(''));
}).catch(function(err) }).catch(function(err)
@ -102,8 +111,8 @@ app.use(function(request, res)
} }
else else
{ {
res.write(html);
res.end();
result.write(html);
result.end();
} }
try try
@ -118,17 +127,21 @@ app.use(function(request, res)
{ {
} }
} }
} }
else else
{ {
// utils.printWrongHost(res);
res.writeHead(418, {});
res.end();
// utils.printWrongHost(result);
result.writeHead(418, {});
result.end();
} }
}); });
//enables gzip compression for the site
app.use(compression());
http.createServer(app).listen(port); http.createServer(app).listen(port);

Loading…
Cancel
Save