24a8bd35 by Rafal Wilinski

Add dynamic title rendered only once, improve PR #25

1 parent c34978d7
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <html> 2 <html>
3 <head> 3 <head>
4 <title>Express Status</title> 4 <title>{{title}}</title>
5 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.min.js"></script> 5 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.bundle.min.js"></script>
6 <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js"></script> 6 <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js"></script>
7 <style> 7 <style>
...@@ -75,7 +75,7 @@ ...@@ -75,7 +75,7 @@
75 <body> 75 <body>
76 <div style="width: 600px; margin: auto"> 76 <div style="width: 600px; margin: auto">
77 <div class="header"> 77 <div class="header">
78 <b>Express Status</b> 78 <b>{{title}}</b>
79 <div id="span-controls" class="span-controls"> 79 <div id="span-controls" class="span-controls">
80 </div> 80 </div>
81 </div> 81 </div>
......
...@@ -73,6 +73,15 @@ ...@@ -73,6 +73,15 @@
73 config.spans = defaultConfig.spans; 73 config.spans = defaultConfig.spans;
74 } 74 }
75 75
76 if (config.title === undefined || !config instanceof String) {
77 config.title = 'Express Status';
78 }
79
80 let renderedHtml;
81 fs.readFile(path.join(__dirname, '/index.html'), function(err, html){
82 renderedHtml = html.toString().replace(/{{title}}/g, config.title);
83 });
84
76 return (req, res, next) => { 85 return (req, res, next) => {
77 if (io === null || io === undefined) { 86 if (io === null || io === undefined) {
78 87
...@@ -92,12 +101,7 @@ ...@@ -92,12 +101,7 @@
92 101
93 const startTime = process.hrtime(); 102 const startTime = process.hrtime();
94 if (req.path === config.path) { 103 if (req.path === config.path) {
95 fs.readFile(path.join(__dirname + '/index.html'), function(err, content) { 104 res.send(renderedHtml);
96 content = content.toString().replace(new RegExp(defaultConfig.title, 'g'), config.title);
97 res.writeHead(200, {'Content-Type': 'text/html'});
98 res.write(content);
99 res.end();
100 });
101 } else { 105 } else {
102 onHeaders(res, () => { 106 onHeaders(res, () => {
103 const diff = process.hrtime(startTime); 107 const diff = process.hrtime(startTime);
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!