Browse Source

Leveraged both client and server side caching.

pull/4/head
jrtechs 5 years ago
parent
commit
b837b5c98f
5 changed files with 82 additions and 46 deletions
  1. +2
    -0
      README.md
  2. +17
    -5
      img/image.js
  3. +20
    -9
      includes/includes.js
  4. +2
    -1
      package.json
  5. +41
    -31
      server.js

+ 2
- 0
README.md View File

@ -75,6 +75,8 @@ npm install crypto
npm install remarkable
npm install markdown
npm install highlight.js
npm install memory-cache --save
```

+ 17
- 5
img/image.js View File

@ -7,13 +7,25 @@ module.exports=
* @param result
* @param fileName
*/
main: function(result, fileName)
main: function(result, fileName, cache)
{
result.contentType = 'image/png';
utils.include("." + fileName).then(function(content)
//result.contentType = 'image/png';
result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'max-age=3600'});
var img = cache.get(fileName);
if(img == null)
{
utils.include("." + fileName).then(function(content)
{
result.write(content);
result.end();
cache.put(content);
});
}
else
{
result.write(content);
result.write(img);
result.end();
});
}
}
};

+ 20
- 9
includes/includes.js View File

@ -41,17 +41,28 @@ module.exports =
* @param path
* @return {*}
*/
sendCSS: function(result, path)
sendCSS: function(result, path, cache)
{
result.writeHead(200, {'Content-Type': 'text/css'});
utils.include("./" + path).then(function(content)
result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control': 'max-age=3600'});
var css = cache.get(path);
if(css == null)
{
result.write(content);
result.end();
}).catch(function(error)
utils.include("./" + path).then(function(content)
{
result.write(content);
result.end();
cache.put(path, content);
}).catch(function(error)
{
console.log(error);
});
}
else
{
console.log(error);
});
result.write(css);
result.end();
}
}
};

+ 2
- 1
package.json View File

@ -7,6 +7,7 @@
"highlight": "^0.2.4",
"markdown": "^0.5.0",
"markdown-to-html": "^0.0.13",
"memory-cache": "^0.2.0",
"mysql": "^2.15.0",
"promise": "^8.0.1",
"sanitizer": "^0.1.3",
@ -31,4 +32,4 @@
"url": "https://github.com/jrtechs/NodeJSBlog/issues"
},
"homepage": "https://github.com/jrtechs/NodeJSBlog#readme"
}
}

+ 41
- 31
server.js View File

@ -26,7 +26,8 @@ app.use(session({ secret: utils.getFileLine('../session_secret'), cookie: { maxA
const port = 8000;
app.use(express.static(__dirname + './', { maxAge: 86400000 }));
const cache = require('memory-cache');
/**
* Parses the request url and calls correct JS files
@ -41,11 +42,11 @@ app.use(function(request, res)
//handles image requests
if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png"))
{
require("./img/image.js").main(res, filename);
require("./img/image.js").main(res, filename, cache);
}
else if(filename.includes("/css/") || filename.includes(".txt"))
{
includes.sendCSS(res, filename)
includes.sendCSS(res, filename, cache)
}
else if(filename.includes("/downloads/"))
{
@ -55,37 +56,48 @@ app.use(function(request, res)
{
var file = "";
if(filename === '' || filename === '/')
{
file="./posts/homePage.js";
}
else
{
var urlSplit = filename.split("/");
if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "./posts/category.js";
else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
file = "./admin/admin.js";
var html = cache.get(filename);
res.writeHead(200, {'Content-Type': 'text/html'});
if(html == null)
{
if(filename === '' || filename === '/')
{
file="./posts/homePage.js";
}
else
file = "./posts/posts.js";
{
var urlSplit = filename.split("/");
if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "./posts/category.js";
else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
file = "./admin/admin.js";
else
file = "./posts/posts.js";
}
Promise.all([includes.printHeader(),
require(file).main(filename, request),
includes.printFooter()]).then(function(content)
{
res.write(content.join(''));
res.end();
cache.put(filename, content.join(''));
}).catch(function(err)
{
console.log(err);
throw err;
});
}
res.writeHead(200, {'Content-Type': 'text/html'});
Promise.all([includes.printHeader(),
require(file).main(filename, request),
includes.printFooter()]).then(function(content)
else
{
res.write(content.join(''));
res.write(html);
res.end();
}).catch(function(err)
{
console.log(err);
throw err;
});
}
}
}
else
@ -93,8 +105,6 @@ app.use(function(request, res)
utils.printWrongHost(res);
res.end();
}
});
http.createServer(app).listen(port);

Loading…
Cancel
Save