0

I've been working on a NodeJS login/register script (called LoginRegister.js) that seemed to work fine in the terminal. I've also installed NodeJS, and the bcrypt module for it. Here's the file:

// Initialization
var fs = require("fs");
var bcrypt = require("bcrypt");
var LUinputID = $("#LUsernameIn").value;
var LPinputID = $("#LPasswordIn").value;
var RUinputID = $("#RUsernameIn").value;
var RPinputID = $("#RPasswordIn").value;

var UserStorageTextFile = "Users.txt";

$(document).ready(function(){
    console.log("Hello");
    var RButton = $("rBTN").addEventListener("click", registerUser(UserStorageTextFile, RUinputID, RPinputID));
    var LButton = $("#lBTN").addEventListener("click", registerUser(UserStorageTextFile, LUinputID, LPinputID));
});

// Defining Functions
function encrypt(passwordFromUser) {
    var salt = bcrypt.genSaltSync(10);
    var hash = bcrypt.hashSync(passwordFromUser, salt);
    return hash;
}

function passCheck(passwordFromUser, passHashed){
    return bcrypt.compareSync(passwordFromUser,passHashed);
}

function loginUser(usersTextFile, username, password){
    data = fs.readFileSync(usersTextFile).toString();

    if(data.indexOf(username) != -1){
        console.log(data.indexOf(username));
        for (var i = 0; i <= data.indexOf(username); i++) {
            if (i == data.indexOf(username)) {
                i += (username.length + 1);
                passcode = data.slice(i, i+60);

                if (passCheck(password, passcode)) {
                    console.log("Yes!!");
                }
                else{
                    console.log("No!!");
                }
            }
        }
    }
    else{
        console.log("No!!");
    }
}

function registerUser(usersTextFile, username, password) {
    data = fs.readFileSync(usersTextFile);
    saveData = data + username + "-" + encrypt(password) + "\n";

    fs.writeFile('Users.txt', saveData, (err2) => {
        console.log("User " + username + " Registered!");
    });
}

I wanted to test it in a browser, so I put together an html file to display my JS one in action:

<!DOCTYPE html>
<html>
<head>
    <title>Authentication Test</title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="LoginRegister.js"></script>
</head>

<h2>Login</h2>
<input type="text" id="RUsernameIn">
<input type="password" id="RPasswordIn">
<button id="lBTN">Login</button>

<h2>Register</h2>
<input type="text" id="LUsernameIn">
<input type="password" id="LPasswordIn">
<button id="rBTN">Register</button>

</html>

I opened up the HTML file in Microsoft Edge and tried pressing the register button after putting in details into the boxes, but I checked the Users.txt file and nothing had happened. After looking at the F12 Developer Tools, I noticed that on startup, the console stated:

SCRIPT5009: 'require' is undefined
LoginRegister.js (2,1)
lllllllllllll
  • 13
  • 2
  • 6
  • 3
    `require()` is a server-side function in node.js. The code you show is running in the browser, not in node.js. It appears you may be confused about how a browser and node.js work together. node.js is used as your web server. The browser requests a specific URL from the web server. Your node.js code receives that request and then, using whatever code you want in node.js, it creates the desired HTML page for that URL and then sends that back as the response to the browser's request. The browser then parses the HTML and displays the page. Any scripts in that page are run in the browser. – jfriend00 Jun 21 '17 at 01:21
  • 1
    This is a possible duplicate for this https://stackoverflow.com/questions/31931614/require-is-not-defined-node-js – Jalil Jun 21 '17 at 01:22
  • You have a mayor misconception. Node is a server-side JS runtime. It will not run in a browser. Both Witten in javascript, different runtime environments. To use node code and node syntax in a browser environment check webpack. – fsaint Jun 21 '17 at 01:24
  • Thanks, I also wanted to ask why when I run this, the console logs: SCRIPT1004: Expected ';' LoginRegister(19, 7) – lllllllllllll Aug 21 '17 at 05:31

1 Answers1

0

Node.JS is a server-side technology, not a browser technology. Thus, Node-specific calls, like require(), do not work in the browser.

See browserify or webpack if you wish to serve browser-specific modules from Node.

See more: require is not defined? node.js Thank Rob Raisch.

Alex
  • 3,646
  • 1
  • 28
  • 25