简体   繁体   中英

How do I run node.js in my Java script code?

I'm quite new to web developing (a week or so in) and I understand I may be moving too quickly but I need some help as I don't quite understand this. What I have so far is a login page that compares the 2 inputs (username + password) to 2 constant variables. However the way I want it to work is comparing the inputs to 2 values I read from a text file, so I can have like a database of usernames/passwords.

 <script>

const adminUserName = "admin";
const adminPassWord = "123";


function getUsername(){
var username = document.getElementById("userName").value;
var password = document.getElementById("passWord").value;

username == adminUserName && password == adminPassWord ? window.open ("admin.html","mywindow"):
document.write("Invalid entry.")

}

and the code I need to add (node.js) is

    var fs = require('fs')
    fs.readFile('loginData.txt', 'utf8', function(err, data){
    })

so how do I connect node.js to my website? Sorry if I haven't explained well, also if you have a better idea for storing user data please feel free to explain (remember im new) :)

Caution before the answer: If you are new to website development and you are needing to make usernames and passwords, I want to caution you against putting vulnerable info pass the username and password wall. There are many sophisticated and non-sophisticated methods for getting behind passwords and usernames. Sometimes, this can be a struggle for experienced programmers. Amateur coding around sensitive info might make you liable for any compromises. Please note that I am not an attorney. Please check out any info I offer in this regard with a qualified, competent, & licensed attorney at law in your jurisdiction.

That said, the super proper way to do what you are trying to do is to use mysql in tandem with php. Rather than write out a fully fleshed out answer on that, I would rather refer you to javascript, php, and msql as a topic.

Now to do what you want to do, this quick and dirty way with no expectation of basic security:

<html>
<head>
<title>Hey</title>
<script>

function sendit()
{
if (document.getElementById("UserName").value == "")
{
alert("Please Enter Something in the Username Field");
}

if (document.getElementById("password1").value == "")
{
alert("Please Enter Something in the Password Field");
}

if (document.getElementById("UserName").value != "" && document.getElementById("password1").value != "")
{
my_form.action = "yourphpfile.php";
document.my_form.submit();

}
}
</script>
</head>
<body>
<h3>Welcome User <br /> Please enter your username and password</h3>
<form method="post" name="my_form">
<h4>User Name: &nbsp; <input type="text" id="UserName" name="UserNameN" /></h4>
<h4>Password: &nbsp; &nbsp; &nbsp;<input type="password" id="password1" name="password1N" /></h4><br />
<input type="button" value="Submit" onClick="sendit()"/>
</form>
</body>
</html>

Then you need to make the php page:

<?PHP
$UserName = $_POST['UserNameN'];
$Password = $_POST['password1N'];

function redirect($url)
{
   header('Location: ' . $url);
   exit();
}

if ($UserName == "BobTim" and $Password == "Password")
{
session_start();
$_SESSION['username']= 'BobTim';
redirect("abc123.html");
exit();
}


if ($UserName == "Blake" and $Password == "12345")
{
session_start();
$_SESSION['username']= 'Blake';
redirect("abc123.html");
exit();
}

if ($UserName == "George" and $Password == "ABCDEFG")
{
session_start();
$_SESSION['username']= 'George';
redirect("abc123.html");
exit();
}

if ($UserName == "Steve" and $Password == "ILikeBlue")
{
session_start();
$_SESSION['username']= 'Steve';
redirect("abc123.html");
exit();
}

if ($UserName == "Coco" and $Password == "IceCream")
{
session_start();
$_SESSION['username']= 'Coco';
redirect("abc123.html");
exit();
}


else {
redirect("sorry.html");
exit();
}
?>

After you redirect to abc123.html, you make abc123.html with this at the top:

<?PHP
session_start();

if (!isset($_SESSION['username'])) {
header('Location: http://www.mybusinesssample.com/forbidden.html');
}

?>

<html>
<head>
<title>Main Page</title>
</head>
<body>
<?PHP
if (isset($_SESSION['username'])) {
echo "Hi {$_SESSION['username']} <br /><a href='unset.php'>Log Out</a><br /> <br />";
}
?>

Now, you need a logout page which in this case is called unset.php:

<?php
session_start();
session_destroy();
header ('Location: index.html');
?>

And if they don't make it through, you need to tell them sorry:

<html>
<head>
<title></title>
<script>

function sendit()
{
if (document.getElementById("UserName").value == "")
{
alert("Please Enter Something in the Username Field");
}

if (document.getElementById("password1").value == "")
{
alert("Please Enter Something in the Password Field");
}

if (document.getElementById("UserName").value != "" && document.getElementById("password1").value != "")
{
my_form.action = "yourphpfile.php";
document.my_form.submit();
}
}

</script>
</head>
<body>
<h3>Welcome User <br /> Please enter your username and password</h3>
<form method="post" name="my_form">
<h4>User Name: &nbsp; <input type="text" id="UserName" name="UserNameN"/></h4>
<h4>Password: &nbsp; &nbsp; &nbsp;<input type="password" id="password1" name="password1N" /></h4><br />
Sorry! Your Username and/or your password wasn't recognized!   <br />
<button type="button" id="button" onclick="sendit()">Submit</button>
</form>
</body>
</html>

Lastly, I would like to thank you for making this old, old project relevant again.

Sounds like you may need to consider creating a desktop app to work with the website.

Reading in a text file with node.js on the client side, is very easy if you use either NW or Electron. These programs take your HTML +CSS + JS and produce an executable app that has access to node.js and a Chromium window.

So your desktop app could then communicate with your website.

NW: https://nwjs.io/

Electron: https://electronjs.org/

I use the former and it works like a charm. In fact the only trouble I ever get is from the countless Chromium bugs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM