Skip to content

Commit d631ecf

Browse files
committed
perf: only load template and stylesheet once
1 parent 7bbfeac commit d631ecf

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ unreleased
33

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

89
1.4.3 / 2016-01-17

index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ var util = require('util')
2525
* @private
2626
*/
2727

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

86-
// load html and style
87-
var html = fs.readFileSync(TEMPLATE_PATH, 'utf8')
88-
var style = fs.readFileSync(STYLESHEET_PATH, 'utf8')
89-
9086
return function errorHandler(err, req, res, next){
9187
// respect err.statusCode
9288
if (err.statusCode) {
@@ -133,8 +129,8 @@ exports = module.exports = function errorHandler(options) {
133129
var stackHtml = stack
134130
.map(function (v) { return '<li>' + escapeHtmlBlock(v) + '</li>' })
135131
.join('')
136-
var body = html
137-
.replace('{style}', style)
132+
var body = TEMPLATE
133+
.replace('{style}', STYLESHEET)
138134
.replace('{stack}', stackHtml)
139135
.replace('{title}', escapeHtml(exports.title))
140136
.replace('{statusCode}', res.statusCode)

0 commit comments

Comments
 (0)