Skip to content

Commit

Permalink
perf: only load template and stylesheet once
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Apr 22, 2016
1 parent 7bbfeac commit d631ecf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -3,6 +3,7 @@ unreleased

* Pretty print JSON error response
* perf: front-load HTML template and stylesheet at middleware construction
* perf: only load template and stylesheet once
* perf: resolve file paths at start up

1.4.3 / 2016-01-17
Expand Down
12 changes: 4 additions & 8 deletions index.js
Expand Up @@ -25,8 +25,8 @@ var util = require('util')
* @private
*/

var TEMPLATE_PATH = path.join(__dirname, '/public/error.html')
var STYLESHEET_PATH = path.join(__dirname, '/public/style.css')
var TEMPLATE = fs.readFileSync(path.join(__dirname, '/public/error.html'), 'utf8')
var STYLESHEET = fs.readFileSync(path.join(__dirname, '/public/style.css'), 'utf8')
var doubleSpaceGlobalRegExp = / /g
var inspect = util.inspect
var newLineGlobalRegExp = /\n/g
Expand Down Expand Up @@ -83,10 +83,6 @@ exports = module.exports = function errorHandler(options) {
log = logerror
}

// load html and style
var html = fs.readFileSync(TEMPLATE_PATH, 'utf8')
var style = fs.readFileSync(STYLESHEET_PATH, 'utf8')

return function errorHandler(err, req, res, next){
// respect err.statusCode
if (err.statusCode) {
Expand Down Expand Up @@ -133,8 +129,8 @@ exports = module.exports = function errorHandler(options) {
var stackHtml = stack
.map(function (v) { return '<li>' + escapeHtmlBlock(v) + '</li>' })
.join('')
var body = html
.replace('{style}', style)
var body = TEMPLATE
.replace('{style}', STYLESHEET)
.replace('{stack}', stackHtml)
.replace('{title}', escapeHtml(exports.title))
.replace('{statusCode}', res.statusCode)
Expand Down

0 comments on commit d631ecf

Please sign in to comment.