Split() function not working correctly in node js


#1

The code below is the server side script of my unpublished website. On processing signup form data, a strange error occurs. The value of m, n, o and p in the code below is logged to console correctly but when these variables are split in arrays e.g.(let users = m.split(’,’)), the value of users is shown null and logged to console like this [’’]. How can it be solved?

var http = require('http');
var url = require('url');
var fs = require('fs');
const { parse } = require('querystring');

let x = function (req, res) {
  var q = url.parse(req.url, true);
  var fs = require('fs');

var dat = "New File Contents";

  if (q.pathname === '/signinform' || q.pathname === '/signupform'){

    let m = '';
    let n = '';
    let o = '';
    let p = '';

    fs.readFile('./4v6RTFYUt765v7T6756/fgYUT7657ghyug786gY67/asghd78sa6dv6786v7guy6786hjgjhHJG.html','utf-8', function(err, data){
      if(err){console.log(err)}
      m = String(data);
      console.log(m);
    });
    
    fs.readFile('./738bdyuhuHJKHjkh897/dgshgdGHJGuy7897kJHIO7ij/hjsua76d89sa7dy87dsaHUIKJH.html','utf-8', function(err, data){
      if(err){console.log(err)}
      n = String(data);
      console.log(n);
    });
    fs.readFile('./hgy657tyhgVUY67867867/hI77897HJKHJH789JKHhh87h/hjijHJKLHh7987.html','utf-8', function(err, data){
      if(err){console.log(err)}
      opq = String(data);
      console.log(o);
    });
    fs.readFile('./jhd89bs7d89sad7j897HJiouh/hjh89789NHUioun89i789/hjuhy8u7688NHYUYhni78989j.html','utf-8', function(err, data){
      if(err){console.log(err)}
      pqr = String(data);
      console.log(p);
    });
    let v = ',';

    let users = m.split(v);
    let passes = n.split(v);
    let emails = o.split(v);
    let dNames = p.split(v);

    function collectRequestData(request, callback) {
      
      const FORM_URLENCODED = 'application/x-www-form-urlencoded';
      
      if(request.headers['content-type'] === FORM_URLENCODED) {
          
        let body = '';
        
        request.on('data', chunk => {
          body += chunk.toString();
        });
        request.on('end', () => {
            callback(parse(body));
        });
      }
      else {
        callback(null);
      }
    }

    if(req.method === 'POST') {

      collectRequestData(req, result => {
      console.log(result);
      
      let i;
      let j;
       
      if (q.pathname === '/signinform'){

      let username = result.user;
      let password = result.pass;
      
      let user;
      let pass;

      checkUser = () => {
        for (i = 0; i < users.length; i++) {
          if(users[i] === username.toLowerCase()){
            user = true;
            break;
          }
          else {
            user = false;
          }
        }
      }
      checkPass = () => {
        for (j = 0; j < passes.length; j++) {
          if(passes[j] === password){
            pass = true;
            break;
          }
          else{
            pass = false;
          }
        }
      }

      function checkLogin(){
       
        checkUser();
        checkPass();
        
        if(username.length >= 3 && password.length >= 6){
          if (user){
            if (pass){
              if (i === j) {
                res.writeHead(301, { "Location": "https://" + req.headers['host'] + '/home' });
                return res.end('You have sucessfully logged in.');
              }
              else {
                res.end('Username and password didn\'t match.');
              }
            }
            else {
              res.end('Incorrect Password.');
            }
          }
          else {
            res.end('Username does not exist.');
          }
        }
        else {
          if (username.value.length < 3){
            res.end('Enter a valid username.');
          }
          if (password.value.length < 6){
            res.end('Please enter a valid password.');
          }
        }
      }
      checkLogin();
      }
      
      else if(q.pathname === '/signupform'){

        let username = result.user;
        let password = result.pass;
        let repassword = result.repass;
        let email = result.email;
        let dName = result.dname;

        let user;
        let pass;
        let remail;
        
        

        checkUser = () => {
          for (i = 0; i < users.length; i++) {
            if(users[i] === username.toLowerCase()){
              user = true;
              break;
            }
            else {
              user = false;
            }
          }
        }

        checkPass = () => {
          if(password === repassword){
            return pass = true;
          }
          else {
            pass = false;
          }
        }

        checkEmail = () => {
          for (i = 0; i < emails.length; i++) {
            if(emails[i] === email.toLowerCase()){
              remail = true;
              break;
            }
            else {
              remail = false;
            }
          }
        }
        
        function checkSignup(){
         
          checkUser();
          checkPass();
          checkEmail();
         
          if (!user){
            if(!remail){
              if(pass){
                console.log(users);
                console.log(passes);
                console.log(emails);
                console.log(dNames);
                res.end('You have sucessfully signed up.');
              }
              else {
                res.end('Passwords did not match.')
              }
            }
            else{
              res.end('Eamil Adress already exists.')
            }
           }
           else {
            res.end('Username already exists.');
           }
          }
        
        checkSignup();
        
     
      }

      });
    }
  }

  var pathArray = q.pathname.split('/');

  let endFile = pathArray[pathArray.length - 1];

  function xyz(pqr){
    
    if (endFile !== 'index.html' && endFile !== 'style.css' && endFile !== 'script.js' && endFile !== 'signupform'){
      return pqr = '.' + q.pathname.toLowerCase() + '/index.html';
    }
    else{
         return pqr = '.' + q.pathname.toLowerCase();
      }
    }
   
  var filename = xyz(filename);

  console.log(filename);
    
  fs.readFile(filename, function (err, data) {
    
    if (err) {
      if (filename === './index.html' || filename === './/index.html'){
        res.writeHead(301, { "Location": "https://" + req.headers['host'] + '/home' });
        return res.end();
      }
      res.writeHead(404, {'Content-Type': 'text/html'});
      return res.end("<b> 404 Not Found </b>");
    }
   
    if (endFile === 'style.css'){
      res.writeHead(200, {'Content-Type': 'text/css'});
      res.write(data);
      return res.end();
    }
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}

http.createServer(x).listen(8080);