Skip to content

Commit

Permalink
static server caching
Browse files Browse the repository at this point in the history
- adds caching to static-server
  • Loading branch information
gabrielcsapo committed Aug 11, 2017
1 parent fb5753e commit 5e79857
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- adds caching to static-server
- abstract models into their own files and their own collections
- fixes the middleware request logger
- fixes CLI responses
Expand Down
13 changes: 12 additions & 1 deletion lib/helpers/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const map = {
'.doc': 'application/msword'
};

const cache = {};

http.createServer((req, res) => {
let { url } = req;

Expand All @@ -30,6 +32,11 @@ http.createServer((req, res) => {
const parsedUrl = Url.parse(url);
let pathname = `.${parsedUrl.pathname}`;

if(cache[pathname]) {
res.setHeader('Content-Type', cache[pathname]['Content-Type']);
return res.end(cache[pathname]['data']);
}

const ext = path.parse(pathname).ext;

fs.exists(pathname, (exist) => {
Expand All @@ -46,8 +53,12 @@ http.createServer((req, res) => {
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
cache[pathname] = {
'Content-Type': map[ext] || 'text/plain',
data
};
// if the file is found, set Content-type and send data
res.setHeader('Content-type', map[ext] || 'text/plain');
res.setHeader('Content-Type', map[ext] || 'text/plain');
res.end(data);
}
});
Expand Down

0 comments on commit 5e79857

Please sign in to comment.