本地简易测BMI的JS


1
/*** 2 * A simple Node JS Server To Calculate BMI 3 * Take height and weight as operand 4 * Example: 5 * http://localhost:8000/calculateBMI 6 * The server responds with 7 * Hi Sunny. You are 25. years old. Your calculated BMI is: 20.959183673469386. You are normal. 8 * To run simply node ./app.js 9 * */ 10 let http = require('http'); 11 var fs = require('fs'); 12 let urlParse = require('url'); 13 const { 14 parse 15 } = require('querystring'); 16 /** 17 * 18 1. http: To create a http based server/client. 19 2. fs: FileSystem module to help us with file/folders related operations 20 3. url: Uniform Resource Locator module to help us with URL related operations 21 */ 22 http.createServer(function (request, response) { 23 let filePath = './index.html'; 24 const { 25 method, 26 url 27 } = request; // get the URL and method from the request 28 // const {url} = request; <==> const url=request.url; 29 //const {method}=request <==> const method=request.method; 30 console.log(url, method); // print them out 31 let q = urlParse.parse(url, true).query; // parse the query part of the URL 32 let pathName = urlParse.parse(url, true).pathname; 33 if (pathName === '/') // if homepage: http://localhost:8000 34 { 35 fs.readFile(filePath, function (error, content) { // send the home page back to the user 36 // use the fs package to read the file index.html from the local drive 37 response.writeHead(200, { 38 'Content-Type': 'text/html' 39 }); 40 response.end(content, 'utf-8'); 41 }); 42 } else if (pathName === '/calculateBMI') { 43 console.log('We got post'); 44 45 if (request.method === 'POST') { 46 let body = ''; 47 request.on('data', chunk => { 48 body += chunk.toString(); 49 }); 50 request.on('end', () => { 51 let items = parse(body); // convert the data into an object. i.e. {height=178,weight=60} 52 let calculatedBMI = 0; 53 /**** 54 * Metric BMI Formulae: - BMI= (weight in Kilograms/ (height in meters*height in meters)) 55 * Less than 18.5 is represents underweight 56 * mid 18.5 -24.9 indicate normal weight 57 * Between 25 -29.9 denotes over weigh 58 * Beyond 30 signifies obesity 59 * */ 60 /** 61 * Using Math.round(num * 100) / 100 function to show BMI value upto 2 decimal. 62 */ 63 64 calculatedBMI = Math.round((items.weight * 10000 / (items.height * items.height)) * 100)/100 65 if(calculatedBMI < 18.5){ 66 response.write('Hi ' + items.name +'. You are '+ items.age +'. years old. Your calculated BMI is: '+ calculatedBMI + '. You are underweight.'); 67 console.log('Your result is: ' + calculatedBMI); 68 } 69 else if(calculatedBMI >=18.5 || calculatedBMI <= 24.9){ 70 response.write('Hi ' + items.name +'. You are '+ items.age +'. years old. Your calculated BMI is: '+ calculatedBMI + '. You are normal.'); 71 console.log('Your result is: ' + calculatedBMI); 72 } 73 else if(calculatedBMI >= 25 || calculatedBMI <= 29.9){ 74 response.write('Hi ' + items.name +'. You are '+ items.age +'. years old. Your calculated BMI is: '+ calculatedBMI + '. You are over weight.'); 75 console.log('Your result is: ' + calculatedBMI); 76 } 77 else if(calculatedBMI >= 30){ 78 response.write('Hi ' + items.name +'. You are '+ items.age +'. years old. Your calculated BMI is: '+ calculatedBMI + '. You weigh much more which signifies.'); 79 console.log('Your result is: ' + calculatedBMI); 80 } 81 response.end(); 82 }); 83 } 84 } else { 85 console.log('We got Error'); 86 response.writeHead(404); 87 response.write('Request not Found'); 88 response.end(); 89 } 90 }).listen(8000); // Setting to default port 8000

Then create an HTML file with a form.

  1 <!DOCTYPE html>
  2 <html>
  3 <head>
  4 <meta name="viewport" content="width=device-width, initial-scale=1">
  5 <!-- Bootstrap CSS CDN -->
  6 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  7 <!-- Bootstrap JS CDN  -->
  8 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  9 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
 10 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
 11 <!-- Internal CSS -->
 12 <style>
 13 body {font-family: Arial, Helvetica, sans-serif;}
 14 * {box-sizing: border-box;}
 15 input[type=text],input[type=number]{
 16   width: 100%;
 17   padding: 12px;
 18   border: 1px solid #ccc;
 19   border-radius: 4px;
 20   box-sizing: border-box;
 21   margin-top: 6px;
 22   margin-bottom: 16px;
 23   resize: vertical;
 24 }
 25 #bmi{
 26   width: 50%;
 27   padding: 12px;
 28   /* padding: 12px;
 29   margin-top: 6px;
 30   margin-bottom: 16px; */
 31   resize: vertical;
 32 }
 33 input[type=submit] {
 34   background-color: #4CAF50;
 35   color: white;
 36   padding: 12px 20px;
 37   border: none;
 38   border-radius: 4px;
 39   cursor: pointer;
 40 }
 41 input[type=submit]:hover {
 42   background-color: #45a049;
 43 }
 44 .container {
 45   border-radius: 5px;
 46   background-color: #f2f2f2;
 47   padding: 20px;
 48 }
 49 .col-25 {
 50   float: left;
 51   width: 25%;
 52   margin-top: 6px;
 53 }
 54 .col-75 {
 55   float: left;
 56   width: 75%;
 57   margin-top: 6px;
 58 }
 59 /* Clear floats after the columns */
 60 .row:after {
 61   content: "";
 62   display: table;
 63   clear: both;
 64 }
 65 /* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
 66 @media screen and (max-width: 600px) {
 67   .col-25, .col-75, input[type=submit] {
 68     width: 100%;
 69     margin-top: 0;
 70   }
 71 }
 72 </style>
 73 </head>
 74 <body>
 75 <br>
 76 <h3 style="text-align: center">BMI Calculator Form</h3>
 77 <!-- A Basic General Information Form -->
 78 <div class="container">
 79         <form action="/calculateBMI" method="post">
 80                 <div class="form-group">
 81                   <label for="name">Name:</label>
 82                   <input type="text" class="form-control" name="name" id="name" aria-describedby="name" placeholder="Enter name">
 83                 </div>
 84                 <div class="form-group">
 85                   <label for="age">Age:</label>
 86                   <input type="number" class="form-control" name="age" id="age" placeholder="Enter age">
 87                 </div>
 88                 <div class="row">
 89                     <div class="form-group col">
 90                         <label for="height">Height [in cm]:</label>
 91                         <input type="number" class="form-control" id="height" name="height" placeholder="Your height in cm.."> 
 92                     </div>
 93                     <div class="form-group col">
 94                         <label for="weight">Weight [in kg]:</label>
 95                         <input type="number" class="form-control" id="weight" name="weight" placeholder="Your weight in kg..">
 96                     </div>
 97                 </div>
 98                 <button type="submit" class="btn btn-primary">Submit</button>
 99               </form>
100 </div>
101 </body>
102 </html>

The eventual outcome has been shown in below screenshot

 

posted @ 2020-08-11 20:11  Skiing_Bison  阅读(88)  评论(0)    收藏  举报