Browse Source

Got the contact page to work with the new routes system.

pull/61/head
jrtechs 5 years ago
parent
commit
857de1222b
16 changed files with 100 additions and 62 deletions
  1. +0
    -1
      blog/contact.js
  2. +63
    -6
      includes/includes.js
  3. +1
    -1
      routes/about.js
  4. +1
    -1
      routes/admin/analytics.js
  5. +1
    -1
      routes/admin/downloads.js
  6. +1
    -1
      routes/admin/login.js
  7. +1
    -1
      routes/admin/posts.js
  8. +1
    -1
      routes/admin/users.js
  9. +1
    -1
      routes/category/index.js
  10. +2
    -2
      routes/contact.js
  11. +6
    -2
      routes/downloads/index.js
  12. +13
    -1
      routes/includes/index.js
  13. +6
    -1
      routes/index.js
  14. +1
    -1
      routes/projects/index.js
  15. +0
    -4
      sites/blog.js
  16. +2
    -37
      utils/utils.js

+ 0
- 1
blog/contact.js View File

@ -215,7 +215,6 @@ module.exports =
const html = whiskers.render(content[0], templateContext); const html = whiskers.render(content[0], templateContext);
result.write(html); result.write(html);
result.end(); result.end();
}).catch(function(err) }).catch(function(err)
{ {
console.log(err); console.log(err);

+ 63
- 6
includes/includes.js View File

@ -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);

+ 1
- 1
routes/about.js View File

@ -1,6 +1,6 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
}); });

+ 1
- 1
routes/admin/analytics.js View File

@ -1,6 +1,6 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
}); });

+ 1
- 1
routes/admin/downloads.js View File

@ -1,6 +1,6 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
}); });

+ 1
- 1
routes/admin/login.js View File

@ -1,6 +1,6 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
}); });

+ 1
- 1
routes/admin/posts.js View File

@ -1,6 +1,6 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
}); });

+ 1
- 1
routes/admin/users.js View File

@ -1,6 +1,6 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
}); });

+ 1
- 1
routes/category/index.js View File

@ -1,6 +1,6 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
}); });

+ 2
- 2
routes/contact.js View File

@ -1,8 +1,8 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
require("../blog/contact.js").main(request, result);
}); });
routes.get('*', (request, result) => routes.get('*', (request, result) =>

+ 6
- 2
routes/downloads/index.js View File

@ -1,8 +1,12 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
{
/** used to parse the request URL */
const url = require('url');
routes.get('/', (request, result) =>
{
const filename = url.parse(request.url, true).pathname;
require("../../includes/downloads.js").main(result, filename);
}); });

+ 13
- 1
routes/includes/index.js View File

@ -1,8 +1,20 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
/** used to parse the request URL */
const url = require('url');
const contentLoader = require('../../includes/staticContentServer.js');
routes.get('*', (request, result) =>
{ {
var filename = url.parse(request.url, true).pathname;
if(!contentLoader.serveStaticContent(request, result, filename, "includes"))
{
//missing content
}
}); });

+ 6
- 1
routes/index.js View File

@ -6,12 +6,17 @@ routes.use('/about', about);
const contact = require('./contact'); const contact = require('./contact');
routes.use('/contact', contact); routes.use('/contact', contact);
const includes = require('./includes');
routes.use('/includes', includes);
const projects = ["/steam/"]; const projects = ["/steam/"];
routes.get('/', (request, result) => routes.get('/', (request, result) =>
{ {
//blog home
//blog home page
}); });

+ 1
- 1
routes/projects/index.js View File

@ -1,6 +1,6 @@
const routes = require('express').Router(); const routes = require('express').Router();
routes.post('/', (request, result) =>
routes.get('/', (request, result) =>
{ {
}); });

+ 0
- 4
sites/blog.js View File

@ -43,10 +43,6 @@ module.exports=
{ {
require("../includes/downloads.js").main(result, filename); require("../includes/downloads.js").main(result, filename);
} }
else if (filename.includes("/contact"))
{
require("../blog/contact.js").main(request, result);
}
else else
{ {
var page = request.query.page; var page = request.query.page;

+ 2
- 37
utils/utils.js View File

@ -6,47 +6,12 @@
//used for file io //used for file io
const fs = require('fs'); const fs = require('fs');
const includes = require("../includes/includes");
module.exports= module.exports=
{ {
/**
* 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)
{
return new Promise(function(resolve, reject)
{
try
{
resolve(fs.readFileSync(fileName));
}
catch (e)
{
console.log("Could not find " + fileName);
resolve("");
}
});
},
includeInObject: function(key, context, fileName)
{
return new Promise(function(resolve, reject)
{
module.exports.include(fileName).then(function(result)
{
context[key] = result;
resolve();
}).catch(function(error)
{
context[key] = "File Not Found";
reject(error);
console.log(error);
})
})
},
/** /**

Loading…
Cancel
Save