All my program is really doing is taking two inputs on the HTML form and having it parsed into text in node and then doing a math operation on the two. The strange thing is that it only happens with printing numbers. Not with strings.
The interesting thing is that it still prints the right number, but it's labeled with the invalid status code. It can be seen at the top of the error.
Here is the HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BMI Calculator</title>
</head>
<body>
<h1>BMI Calculator</h1>
<form action="/bmicalculator" method="post">
<input type="number" name="height" placeholder="Height">
<input type="number" name="weight" placeholder="Weight">
<button type="submit" name="submit">Submit</button>
</form>
</body>
</html>
Here is the js code:
const express = require('express');
const bodyParser = require('body-parser');
const res = require('express/lib/response');
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', function(req, res) {
res.sendFile(__dirname '/index.html');
});
app.get('/bmicalculator', function(req, res) {
res.sendFile(__dirname '/bmiCalculator.html');
});
app.post('/', function(req, res) {
let num1 = Number(req.body.num1);
let num2 = Number(req.body.num2);
let r = num1 num2;
res.send(r);
})
app.post('/bmicalculator', function(req, res) {
let weight = Number(req.body.weight);
let height = Number(req.body.height);
let r = weight * height;
res.send(weight);
})
app.listen(3000, function() {
console.log('server started');
});
And here is the error:
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: 12
at new NodeError (node:internal/errors:371:5)
at ServerResponse.writeHead (node:_http_server:274:11)
at ServerResponse._implicitHeader (node:_http_server:265:8)
at ServerResponse.end (node:_http_outgoing:871:10)
at ServerResponse.send (/Users/user/Desktop/Calculator/node_modules/express/lib/response.js:221:10)
at /Users/user/Desktop/Calculator/calculator.js:28:9
at Layer.handle [as handle_request] (/Users/user/Desktop/Calculator/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/user/Desktop/Calculator/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/user/Desktop/Calculator/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/user/Desktop/Calculator/node_modules/express/lib/router/layer.js:95:5)
CodePudding user response:
You should use this:
app.post('/', function(req, res) {
let num1 = Number(req.body.num1);
let num2 = Number(req.body.num2);
let r = num1 num2;
res.send(`${r}`);
})
app.post('/bmicalculator', function(req, res) {
let weight = Number(req.body.weight);
let height = Number(req.body.height);
let r = weight * height;
res.send(`${weight}`);
})