Username and password validation in Node.js

i have created database named 'USERS' and also created 'USERS' table inside the database it looks like

enter image description here

now i run 'localhost:8000'. i got my login.html and also 'connected' message in console. now i want to know After Entering username password in login.html i want to validate usename password on 'verify' button click. how can i do that????? pls Help me.

Server.js:

var http=require('http');
var express=require('express');
var fs=require('fs');
var mysql=require('mysql');
var app=express();
var server=http.createServer(app);
server.listen(8000);
var connection=mysql.createConnection({
host:'localhost',
user:'root',
password:'root'
});
connection.connect(function(err) {
if(!err)
 {
 console.log('connected');
 }
});
app.get('/', function(request,response){
fs.readFile('login.html', function(Error,Res){
if(!Error)
{
response.writeHead(200,{'content-type':'text/html'});
response.write(Res);
}
});
});

login.html:

<html>
<head>
<title>Angular Js</title>
<script src="assets/js/Angular.min.js"></script>
<style>
</style>
<body>
<input id="username" />
<input id="password" />
<button id="submit">Verify</button>
</body>
</html>

You need to make a few changes to your code..

As a first step, you will need to add in some middleware for the Express framework to handle post data - something like:

var app=express();
var bodyParser = require('body-parser')
app.use( bodyParser.urlencoded() ); 

Starting with the html page, your input fields need to be wrapped in a form - otherwise they won't be / can't be posted to anything when you click Submit.

<body>
    <form id="someform" action="/login" method="post">
        <input id="username" />
        <input id="password" />
        <button id="submit">Verify</button>
    </form>
</body>

Then, add a new method in your server.js to handle a post request to the /login url, in which you would then be able to read the submitted values & do something appropriate with them.

app.post('/login', function(sReq, sRes) {
        var username = sReq.body.username;
        var password = sReq.body.password;

        if (username=='myusername' && password == 'mypassword') {
               // do something here with a valid login

        } else { 
               // user or password doesn't match
        }
});

Have a look at how to get POST query in express node.js? for some more hints.

This example doesn't do anything around comparing submitted values to the database or encrypting passwords, but might help you get started with submitting post data and accessing it in Node.

It's also not something you should ever do in something you're planning on using in a production setting - passwords should be encrypted, and you'd need a lot of other stuff around handling session state.

By far the best option is to find and use some pre-built authentication middleware - and configure it to suit your needs.