jeudi 6 octobre 2016

Login page: Client to server request being sent twice? Node.js, AngularJS, express framework

This is the client-side AngularJS code snippet:

<form name="login" ng-submit="login()">
    <div class="login" ng-app="loginPage" ng-controller="loginController">
        <input type="text" placeholder="username" ng-model="uname" name="userid"><br>
        <input type="password" placeholder="password" ng-model="pword" name="pswrd"><br>
        <!--<input type="button" ng-click="login();" onclick="check(this.form)" value="Login"/>-->
        <button ng-click="login();">Login</button>
    </div>
</form>

<script language="javascript">
    var app = angular.module('loginPage', []);
    app.controller('loginController', function($scope, $http, $window) {
        console.log("inside controller");
        $scope.login = function() {
            console.log("inside the login function");
            console.log($scope.uname);

            var login_http = $http({
                method: 'POST',
                url: '/login',
                params: { username: $scope.uname, password: $scope.pword }
            }).then(
                    function (response) {
                        $window.alert('successfull!!');
                        //alert('successfull ...');
                    }, function (response) {
                        $window.alert('wrong username/password');
                        //alert('wrong username/password ... ');
                    }
            )
        }

This is the server-side node.js code:

connection.connect();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.sendFile(path.join(__dirname, '../', 'views', 'login.html'));
});
router.post('/login', function(req,res)
{

  console.log("Username:"+req.query.username);
  console.log("Password:"+req.query.password);

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

  connection.query("select * from user where login_name = ?", user, function(err, rows, fields) {
    if (!err){
        if(user==rows[0].login_name && pass==rows[0].pass){
             console.log("success");
             res.sendStatus(200);
            //res.sendFile(path.join(__dirname, '../', 'views', 'team_list.html'));
        }
    }
    else{
        console.log('Error while performing Query.', err);
        res.sendStatus(401);
    }

I am working in WebStorm and I run app.js which says it's listening to localhost:8000. I go to localhost:8000 on my browser. Type in test for username and test for password (I already have this in my sql database). It says "success!"

Meanwhile, my console (while I am running app.js) in webstorm, says this:

sometimes says this:

Server listening on port 8000
GET / 200 10.853 ms - 5260
Username:test
Password:test
success
POST /login?password=test&username=test 200 12.559 ms - 2
GET /?userid=test&pswrd=test 200 10.027 ms - 5260

and sometimes says this:

Server listening on port 8000
GET / 304 16.586 ms - -
Username:test
Password:test
success
POST /login?password=test&username=test 200 12.778 ms - 2
GET /?userid=test&pswrd=test 304 9.742 ms - -

This makes me think that the request is being sent twice. Ideally I would want to console to only do the POST and not the GET. How do I fix this. I'm pretty sure the error is in the angularJS code and I'm doing something wrong with the ng-submit.




Aucun commentaire:

Enregistrer un commentaire