如何使用自己的node.js脚本进行计算?
我正在处理一个计算体重和体重BMI的表格。我已经用Javascript创建了该函数,但是现在我想通过GET请求而不使用Node.js Express包将其转换为Node.js脚本。我必须在动作表单中使用自己的脚本
我创建了server.js文件,并且在此文件中,我创建了处理程序函数和BMI函数(将计算BMI)。我在/?gewicht=10&groesse=10&Name=sdfsd&Email=waqasumer30%40gmail.com&anmerk=
server.js
var http = require('http');
function handler(req, res) {
console.log("sss");
console.log(req.params);
console.log(req.params);
console.log(req.url);
console.log("end")
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
};
function BMI() {
var weight = document.getElementById("gewicht").value;
var height = document.getElementById("groesse").value;
var bodyMassIndex = (weight / (height * height / 10000)).toFixed(2);
document.getElementById("result").innerHTML = "Ihr Body Mass Index beträgt " + bodyMassIndex;
}
http.createServer(handler).listen(8000, function (err) {
if (err) {
console.log('Error starting http server');
} else {
console.log("Server running at http://127.0.0.1:8000/ or http://localhost:8000/");
};
});
Index.html
<head>
<meta charset="UTF-8">
<title>Übung7 :Aufgabe1</title>
<link rel="stylesheet" href="./css/style.css" />
</head>
<body>
<h1>BMI-Rechner</h1>
<form action="http://localhost:8000" method="GET">
<fieldset>
<legend>Angaben zur BMI-Berechnung</legend>
<p><label>Gewicht</label><input type="number" name="gewicht" required /></p>
<p><label>Größe</label><input type="number" name="groesse" min="0.00" step="0.01" required /></p>
<fieldset>
<legend>Geschlecht</legend>
<input type="radio" name="Geschlecht" value="Männlich" /><label>Männlich</label>
<input type="radio" name="Geschlecht" value="Weiblich" /><label>Weiblich</label>
<input type="radio" name="Geschlecht" value="Sonstiges"><label>Sonstiges</label>
</fieldset>
<p><label>Name</label><input type="text" name="Name" placeholder="Max Muster" pattern="^[a-zA-Z]+( [a-zA-Z]+)*$"
title="Enter only letters" required /></p>
<p><label>Email</label><input type="text" name="Email" placeholder="max.muster@gmail.com"
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" required /></p>
<p><label>Anmerkungen</label><textarea name="anmerk" rows="15" cols="50"></textarea></p>
<div style="text-align: right">
<button type="submit">Absenden</button>
<button type="button">Zurücksetzen</button>
</div>
<br>
</fieldset>
<p id="result">Ihr Body Mass Index beträgt___.</p>
</form>
</body>
我正在这样的控制台中获取输入值
/?gewicht = 10&groesse = 10&Name = sdfsd&Email = waqasumer30%40gmail.com&anmerk =