1

I'm trying out the code basics and want to write some basic client-server app. I have an HTML page where user inputs two numbers (num1 and num2) then it passes to JS which passes it to HTTP server written with NodeJS. On the server the numbers should be added and returned to the HTML page. But the server returns this error:

ReferenceError: num1 is not defined

What is wrong with the code?

Here is the JS code:

function myFunction(num1, num2) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      num1 = Math.floor(num1);
      num2 = Math.floor(num2);
      document.getElementById("result").innerHTML = this.responseText;
    }
  };

  xhttp.open("GET", "http://localhost:8080?num1=2&num2=3", true);
  xhttp.send();
}

And here is the NodeJS code:

var http = require('http');

http.createServer(function (req, res) {
  var resnum = 2 + req.params(num1) + req.params(num2);
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write(resnum);
  res.end();
}).listen(8080);
freginold
  • 3,946
  • 3
  • 13
  • 28
IGOR LEVKOVSKY
  • 167
  • 1
  • 4
  • 14
  • 1
    Use `req.query.num1;`. `req.params` is only to get a variable/parameter from the url. – Eyzi Nov 21 '17 at 14:32
  • This will help you for sure https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js – A l w a y s S u n n y Nov 21 '17 at 14:38
  • @Eyzi Get a variable/parameter from the url sound exactly what i need, isn't it? Anyway, req.query doesn'twork neither, gives the same error. – IGOR LEVKOVSKY Nov 21 '17 at 14:43

2 Answers2

1

You have to use the url module https://nodejs.org/api/http.html#http_message_url

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {
  var params = url.parse(req.url, true).query;

  var resnum = 2 + params.num1 + params.num2; //or 2 + parseInt(params.num1) + parseInt(params.num2)
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write(resnum);
  res.end();
}).listen(8080);
LMokrane
  • 826
  • 6
  • 15
1

If you want a concise code like yours you need to use some module like Express framework.

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  const resnum = 2 + parseInt(req.query.num1) + parseInt(req.query.num2);
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(resnum.toString())
})

app.listen(8080)

When you are using 'http' module only, the only thing you have to work with is req.url. You could try hard and get the parameters by breaking down the url but you would have a lengthy code:

var http = require('http');

http.createServer(function (req, res) {
  const step1 = req.url.split('?')[1] //step1 = num1=2&num2=3
  const step2 = step1.split('&') // step2 = [num1=2,num2=3]
  let result = {};

  step2.forEach((val) => { //break down strings further and put into result object
    const value = val.split('=')
    result[value[0]] = value[1]
  })
  var resnum = 2 + parseInt(result.num1) + parseInt(result.num2);
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(resnum.toString());
}).listen(8080);

Some notes:

  • You get that error because num1 is a variable argument to a function. However we don't have a variable num1 declared.

  • Parameters come as strings so unless you parse them into integers, you will have string concatenation and 223 as a result

  • res.write and res.end need a string input so you need to parse back to string after calculations.
Eduard Avetisyan
  • 471
  • 1
  • 4
  • 20