Solution 1 :

You are passing queryparams in your request, so to get those data, you need to write

var username = req.query.user;
var password = req.query.pass;   

& not params

And set status of response before sending response from server side

res.status(200).send(recordset);

Problem :

Hello I recently started learning node js. I have the code showing below and that I want is html button to call a client-side function that this function make a GET request in my server-site function and return the result.

Client-Site

function login(userIn,passIn){
  var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
      if (xhr.readyState !== 4) return;
      if (xhr.status >= 200 && xhr.status < 300) {
        const obj = JSON.parse(xhr.responseText);
        console.log(obj);
      }     
      else 
        console.log('error', xhr);    
  }
  xhr.open('GET','/login?user='+userIn+'&pass='+passIn, false);
  xhr.send();
}

Server-Site (login.js)

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

app.get('/login', function (req, res) {
    var username = req.params.user;
    var password = req.params.pass;   
    var sql = require("mssql");

    // config for your database
    var config = {
        ... 
    };
    // connect to your database
    sql.connect(config, function (err) {    
        if (err) console.log(err);
            // create Request object
        var request = new sql.Request();               
        // query to the database and get the records
        request.query('select * from admins', function (err, recordset) {
           if (err) console.log(err)
           // send records as a response
           res.send(recordset);
           sql.close();
       });
   });
});

const webserver = app.listen(8080, function () {
    console.log("running...");
});

Directory

Project
  node_modules
  www
      .html
      client.js
  login.js (server site function)

I think that my error is in the url of the client function.

Comments

Comment posted by Monstermania

Thanks for the answer. What about the url of the request, based on the directory map provided?

Comment posted by Sarfraaz

Seems fine to me, can you share the exact error you are facing ?

Comment posted by Monstermania

I am not getting an error. If I console.log on server-site, it prints the data from databse. However I am not get any response to client-site.

By