getting a 500 error code on post request at a different route

This is a simple bmi calculator and a normal calculator that can add numbers. The html form loads correct to the browser but then once I submit the form, I get a 500 error code on /bmicalculator route alone.
The normal calculator route is working fine ("/").

Error code :

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code:
at ServerResponse.writeHead (_http_server.js:255:11)
at ServerResponse._implicitHeader (http_server.js:246:8)
at write
at ServerResponse.end (_http_outgoing.js:799:5)
at ServerResponse.send (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\response.js:221:10)
at G:\WebDevelopmentFolder\Calculator\calculator.js:36:7
at Layer.handle [as handle_request] (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\router\layer.js:95:5)
at next (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (G:\WebDevelopmentFolder\Calculator\node_modules\express\lib\router\layer.js:95:5)


<!DOCTYPE html>
<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>BMI Calculator</title>
    <h1>BMI Calculator</h1>
    <form action="/bmicalculator" method="post">
      <input type="text" name="weight" placeholder="weight">
      <input type="text" name="height" placeholder="height">
      <button type="submit">Calculate BMI</button>


//jshint esversion:6

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.urlencoded({extended: true}));

//sending response
app.get("/",function(req, res){
  res.sendFile(__dirname + "/index.html");


  var num1 = Number(req.body.num1);
  var num2 = Number(req.body.num2);
  var result = num1 + num2;

  res.send("The result is: "+result);

app.get("/bmicalculator",function(req, res){
  res.sendFile(__dirname + "/bmicalculator.html");

//post"/bmicalculator",function(req, res){

  var weight = parseFloat(req.body.weight);
  var height = parseFloat(req.body.height);
  var bmi = weight/(height * height);

  res.send("Your BMI is: ", bmi);

//listen to port 3000
app.listen(3000, function(){
  console.log("Server listening on port 3000");

2 thoughts on “getting a 500 error code on post request at a different route”

  1. do this res.send("Your BMI is: "+ bmi); instead of using , like res.send("Your BMI is: ", bmi);

    Because the first params of send() is stand for status code and put content as second parameter.

    .send(200, "Your BMI is: "+ bmi)
  2. please rewrite the this as res.send(`Your BMI is: ${bmi}`)

    Error in your code
    res.send‘s second argument is the status code and a status code cannot be a float


Leave a Comment