|
@ -30,6 +30,25 @@ const crypto = require('crypto'); |
|
|
//caching program to make the application run faster
|
|
|
//caching program to make the application run faster
|
|
|
const cache = require('memory-cache'); |
|
|
const cache = require('memory-cache'); |
|
|
|
|
|
|
|
|
|
|
|
const fs = require('fs'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const readFile = function(filename) |
|
|
|
|
|
{ |
|
|
|
|
|
return new Promise(function(resolve, reject) |
|
|
|
|
|
{ |
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
resolve(fs.readFileSync(filename)); |
|
|
|
|
|
} |
|
|
|
|
|
catch (e) |
|
|
|
|
|
{ |
|
|
|
|
|
console.log(e); |
|
|
|
|
|
console.log("Could not find " + filename); |
|
|
|
|
|
return(""); |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Sends a static file to the client in a way which the web browser |
|
|
* Sends a static file to the client in a way which the web browser |
|
@ -46,7 +65,7 @@ const sendCachedContent = function(path, type, result) |
|
|
|
|
|
|
|
|
if(goods == null) |
|
|
if(goods == null) |
|
|
{ |
|
|
{ |
|
|
utils.include("." + path).then(function(content) |
|
|
|
|
|
|
|
|
readFile(path).then(function(content) |
|
|
{ |
|
|
{ |
|
|
const eTag = crypto.createHash('md5').update(content).digest('hex'); |
|
|
const eTag = crypto.createHash('md5').update(content).digest('hex'); |
|
|
result.writeHead(200, {'Content-Type': type, 'Cache-Control': |
|
|
result.writeHead(200, {'Content-Type': type, 'Cache-Control': |
|
@ -83,10 +102,48 @@ module.exports = |
|
|
*/ |
|
|
*/ |
|
|
printHeader: function(templateContext) |
|
|
printHeader: function(templateContext) |
|
|
{ |
|
|
{ |
|
|
return utils.includeInObject(HEADER_KEY, templateContext, HEADER_FILE); |
|
|
|
|
|
|
|
|
return module.exports.includeInObject(HEADER_KEY, templateContext, HEADER_FILE); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
includeInObject: function(key, context, fileName) |
|
|
|
|
|
{ |
|
|
|
|
|
return new Promise(function(resolve, reject) |
|
|
|
|
|
{ |
|
|
|
|
|
readFile(fileName).then(function(result) |
|
|
|
|
|
{ |
|
|
|
|
|
console.log(result); |
|
|
|
|
|
context[key] = result; |
|
|
|
|
|
resolve(); |
|
|
|
|
|
}).catch(function(error) |
|
|
|
|
|
{ |
|
|
|
|
|
context[key] = "File Not Found"; |
|
|
|
|
|
reject(error); |
|
|
|
|
|
console.log(error); |
|
|
|
|
|
}) |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* A function similar to the include statement in PHP |
|
|
|
|
|
* This function writes a file to the output |
|
|
|
|
|
* |
|
|
|
|
|
* @param fileName the file to append to the result |
|
|
|
|
|
*/ |
|
|
|
|
|
include: function(fileName) |
|
|
|
|
|
{ |
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
return(fs.readFileSync(fileName)); |
|
|
|
|
|
} |
|
|
|
|
|
catch (e) |
|
|
|
|
|
{ |
|
|
|
|
|
console.log("Could not find " + fileName); |
|
|
|
|
|
return(""); |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Appends the footer to the result object |
|
|
* Appends the footer to the result object |
|
|
* |
|
|
* |
|
@ -94,7 +151,7 @@ module.exports = |
|
|
*/ |
|
|
*/ |
|
|
printFooter: function(templateContext) |
|
|
printFooter: function(templateContext) |
|
|
{ |
|
|
{ |
|
|
return utils.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE); |
|
|
|
|
|
|
|
|
return module.exports.includeInObject(FOOTER_KEY, templateContext, FOOTER_FILE); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -114,7 +171,7 @@ module.exports = |
|
|
* @param path |
|
|
* @param path |
|
|
* @return {*} |
|
|
* @return {*} |
|
|
*/ |
|
|
*/ |
|
|
sendCSS: function(result, path, cache) |
|
|
|
|
|
|
|
|
sendCSS: function(result, path) |
|
|
{ |
|
|
{ |
|
|
sendCachedContent(path, 'text/css', result); |
|
|
sendCachedContent(path, 'text/css', result); |
|
|
}, |
|
|
}, |
|
@ -145,7 +202,7 @@ module.exports = |
|
|
|
|
|
|
|
|
fetchTemplate: function(templateName) |
|
|
fetchTemplate: function(templateName) |
|
|
{ |
|
|
{ |
|
|
return utils.include("templates/" + templateName); |
|
|
|
|
|
|
|
|
return readFile("templates/" + templateName); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -156,7 +213,7 @@ module.exports = |
|
|
*/ |
|
|
*/ |
|
|
sendHTML: function(result, fileName) |
|
|
sendHTML: function(result, fileName) |
|
|
{ |
|
|
{ |
|
|
utils.include("." + fileName).then(function(content) |
|
|
|
|
|
|
|
|
readFile("." + fileName).then(function(content) |
|
|
{ |
|
|
{ |
|
|
result.writeHead(200, {'Content-Type': 'text/html'}); |
|
|
result.writeHead(200, {'Content-Type': 'text/html'}); |
|
|
result.write(content); |
|
|
result.write(content); |
|
|