Skip to content

Commit

Permalink
feat: create a serveHttpErrorFile method that serves a custom user de…
Browse files Browse the repository at this point in the history
…fined http error file for a giv

it serves an internal HttpError file if user did not define any file for the given error code in the
.rsvrc.json config file
  • Loading branch information
teclone committed Jul 21, 2018
1 parent 0c71a92 commit fbd7753
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/modules/StaticFileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class {
*@param {string} cacheControl - cache control header for static files
*/
constructor(rootDir, publicPaths, mimeTypes, defaultDocuments, cacheControl) {
this.rootDir = rootDir;
this.publicPaths = publicPaths.map(publicPath => {
return path.join(rootDir, publicPath, '/');
});
Expand Down Expand Up @@ -166,4 +167,31 @@ export default class {
fs.readFileSync(filePath));
}
}

/**
* servers server http error files. such as 504, 404, etc
*@param {http.ServerResponse} response - the response object
*@param {number} status - the response status code
*@param {string} baseDir - the user defined httErors base directory relative to root.
*@param {string} filePath - the file path that is mapped to the error code
*/
serveHttpErrorFile(response, status, baseDir, filePath) {
if (!filePath)
filePath = '../httpErrors/' + status + '.html';
else
filePath = path.join(this.rootDir, '/', baseDir, '/', filePath);

let contentType = this.mimeTypes[path.parse(filePath).ext.substring(1)] || 'text/plain';
return new Promise((resolve) => {
fs.readFile(filePath, (err, buffer) => {
if (err)
buffer = null;

response.writeHead(status, {'Content-Type': contentType});
response.end(buffer);

resolve(response);
});
});
}
}

0 comments on commit fbd7753

Please sign in to comment.