Do circle and trapezium calculation by generating page into memory buffer first
authorNick Downing <nick@ndcode.org>
Sat, 19 Jan 2019 02:28:16 +0000 (13:28 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 19 Jan 2019 02:28:16 +0000 (13:28 +1100)
calculate_button_trapezium.html.jst
calculatebutton.html.jst

index 1182d22..efcd1e3 100644 (file)
@@ -17,10 +17,9 @@ return async env => {
     let b_length = parseFloat(query.b_length)
     let height = parseFloat(query.height)
     let area = (a_length + b_length) / 2 * height
-   
-    env.site.serve(
-      env,
-      200,
+
+    let _out = []
+    _out.push(
       `<!DOCTYPE html>
 <html>
 <head>
@@ -34,14 +33,18 @@ return async env => {
 <h1>The area is: ${area} units<sup>2</sup></h1>
 </body>
 </html>
-`,
-      'calculatebutton.html.jst'
+`
     )
-  }
-  else {
     env.site.serve(
       env,
       200,
+      Buffer.from(_out.join('')),
+      'calculate_button_trapezium.html.jst'
+    )
+  }
+  else {
+    let _out = []
+    _out.push(
       `<!DOCTYPE html>
 <html>
 <head>
@@ -52,7 +55,12 @@ return async env => {
 <h1>Error - Unable to Calculate</h1>
 </body>
 </html>
-`,
+`
+    )
+    env.site.serve(
+      env,
+      200,
+      Buffer.from(_out.join('')),
       'calculatebutton.html.jst'
     )
   }
index f8e9672..449d1a5 100644 (file)
@@ -20,10 +20,9 @@ return async env => {
     let area = Math.PI * radius * radius
     let circumference = Math.PI * radius * 2
     let diameter = radius * 2
-    if (radius < 0) {
-      env.site.serve(
-        env,
-        200,
+    if (isNaN(radius) || radius < 0) {
+      _out = []
+      _out.push(
         `<!DOCTYPE html>
 <html>
 <head>
@@ -31,17 +30,21 @@ return async env => {
 <title>Calculation</title>
 </head>
 <body class="olly">
-<h1>Error - Unable to Calculate Negative Radius</h1>
+<h1>Error - Unable to Calculate Missing or Negative Radius</h1>
 </body>
 </html>
 `,
-        'calculatebutton.html.jst'
       )
-    }
-    else {
       env.site.serve(
         env,
         200,
+        Buffer.from(_out.join('')),
+        'calculatebutton.html.jst'
+      )
+    }
+    else {
+      _out = []
+      _out.push(
       `<!DOCTYPE html>
 <html>
 <head>
@@ -55,15 +58,19 @@ return async env => {
 <h1>The diameter is: ${diameter}  ${units}</h1>
 </body>
 </html>
-`,
+`
+      )
+      env.site.serve(
+        env,
+        200,
+        Buffer.from(_out.join('')),
         'calculatebutton.html.jst'
       )
     }
   }
   else {
-    env.site.serve(
-      env,
-      200,
+    _out = []
+    _out.push(
       `<!DOCTYPE html>
 <html>
 <head>
@@ -71,10 +78,15 @@ return async env => {
 <title>Calculation</title>
 </head>
 <body class="olly">
-<h1>Error - Unable to Calculate</h1>
+<h1>Please go <a href="/circle.html">here</a>.</h1>
 </body>
 </html>
-`,
+`
+    )
+    env.site.serve(
+      env,
+      200,
+      Buffer.from(_out.join('')),
       'calculatebutton.html.jst'
     )
   }