Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2015 22:15
Show Gist options
  • Save anonymous/a39603f3ee74694e8186 to your computer and use it in GitHub Desktop.
Save anonymous/a39603f3ee74694e8186 to your computer and use it in GitHub Desktop.
/* General Request Handler */
//DEFINE MODULE ASSISTANT VARIABLES
var types = {
'' : 'text/html',
'.html' : 'text/html',
'.js' : 'text/javascript',
'.json' : 'application/json',
'.css' : 'text/css',
'.jpg' : 'image/jpeg',
'.png' : 'image/png',
'.eot' : 'application/vnd.ms-fontobject',
'.woff' : 'application/font-woff',
'.svg' : 'text/xml',
'.ttf' : 'application/octet-stream',
'.ico' : 'image/x-icon'
};
//DEFINE MODULE ASSISTANT FUNCTIONS
// Get the extension based on the path
function getExtension(path) {
//Handle index request
if (path == "/") return '';
//Handle other
var i = path.lastIndexOf('.');
return (i < 0) ? '' : path.substr(i);
}
// Get the MIME type of file based on extension
function getType(extension) {
// Return either the MIME type or false
return types[extension];
}
//REQUIRE NPM MODULES
var fs = require('fs');
//GET DIRECT WORKING PATH
var dirPath = process.cwd();
//REQUIRE CUSTOM MODULES
var debug = new (require(dirPath + "/custom_modules/debug"))("General Request Handler");
//CHECK FOR HOME REQUEST
//Convert home request "/" to "index.html"
function checkHomeReq(path){
if (path === "/")
path = "/index.html";
return path;
}
//HANDLE GENERAL REQUEST
var genReqHandler = function(req, res, reqInfo) {
//Get path and check for home request "/"
var path = reqInfo.path;
path = checkHomeReq(path);
var fullPath = dirPath + "/Client" + path;
fs.readFile(fullPath, function(err, data) {
// If there is an error, return 404
if (err) {
res.writeHead(404);
res.end();
debug.log("File denied: " + fullPath);
} else {
var length = data.length;
var extension = getExtension(path);
var type = getType(extension);
// If the type doesn't match a MIME type that this app serves, return 404
if (!type) {
res.writeHead(404);
res.end();
debug.log("File denied: " + fullPath);
// Otherwise, serve the file
} else {
res.writeHead(200, {
'Content-Length' : length,
'Content-Type' : type
});
res.write(data);
res.end();
debug.log("File served: " + fullPath);
}
}
});
};
//DEFINE MODULE EXPORTS
module.exports.handle = genReqHandler;
/* Server */
//REQUIRE NPM MODULES
var fs = require('fs'), https = require('https'), http = require("http"), url = require('url'), path = require('path');
//GET DIRECT WORKING PATH
var dirPath = process.cwd();
//REQUIRE CUSTOM MODULES
var preloadReqHandler = require(dirPath + '/custom_modules/request_handlers/preloaded_request').handle;
var generalReqHandler = require(dirPath + '/custom_modules/request_handlers/general_request').handle;
var debug = new (require(dirPath + "/custom_modules/debug"))("Server");
//Preload requests
var preload = require(dirPath + '/custom_modules/preload').init();
//INIT MODULE
debug.log("Initialized...");
//DEFINE MODULE VARIABLES
var options = {
//key : fs.readFileSync(dirPath + 'SSL/enviziion_ssl_private_key.pem'),
//cert : fs.readFileSync(dirPath + 'SSL/evisiion_ssl_cert.pem')
};
//DEFINE MODULE EXPORTS
//serve files
module.exports.serve = function(port) {
// SSL Disabled
//var server = https.createServer(options, function(req, res) {
var server = http.createServer(function(req, res) {
// Parse & process URL
var reqInfo = url.parse(req.url, true, true), path = reqInfo.pathname;
debug.log("Client [" + req.connection.remoteAddress + "]requesting resource: " + path);
// Quickly handle preloaded requests
if (preloaded[path])
preloadReqHandler(req, res, preloaded[path], path);
// Handle general requests
else
generalReqHandler(req, res, reqInfo);
}).listen(port);
return server;
};
//redirect http to https
module.exports.redirect = function(port) {
var server = http.createServer(function(req, res) {
res.writeHead(301, {
'Content-Type' : 'text/plain',
'Location' : 'https://' + req.headers.host + req.url
});
res.end('Redirecting to SSL\n');
}).listen(port);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment