0
var express = require('express');
var PORT = process.env.PORT||3000;
var _ = require('underscore');
var db = require('./db.js');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
app.use(express.static(__dirname+'/public'));

app.post('/users/login',function(req,res){
console.log("Inside post method. Trying to redirect");
        return res.redirect(301,'/home.html');
  });


db.sequelize.sync().then(function() {
    app.listen(PORT, function() {
        console.log('Express listening on port ' + PORT + '!');
    });
});

Redirect should redirect to a new page home.html after a post request but its not working. Not sure what i am doing wrong here. Before redirecting i was reading user detail from db which I have deleted in this code. index.html

<form ng-app="loginForm" name="userForm" ng-controller="loginController" novalidate>
   <div >
       <label name="email">Email</label>
       <input type="email" style="text-align:center;" name="email" ng-model="user.email" required>
       <span style="color:red" ng-show="userForm.email.$dirty && userForm.email.$invalid && userForm.email.$touched"> Invalid Email address </span> <p style="color:red"> {{email}} </p>
  </div>
  <div>
   <label name="password">Password</label>
   <input type="password" style="text-align:center;"name="password" ng-model="user.password"/>
   <span style="color:red" ng-show="userForm.password.$dirty && userForm.password.$invalid && userForm.password.$touched"> Invalid password</span> <p style="color:red"> {{password}} </p>
   <p style="color:red">{{mess}}</p>
   <input type="submit" value="Login" ng-click="login()" />
 </div>
 </form>
 </div>

Controller:

Service using factory:

angular.module('httpService',[]).
factory('TCPService',['$http',function($http){
return {
  login : function(user) {
    return $http.post('/users/login',user);
  },
  create : function(user) {
  return  $http.post('/users', user);
  }
}
}]);

Controller:

angular.module('logControllers',[])
.controller('loginController',['$scope','$http','TCPService','$window',
function($scope,$http,TCPService,$window){
$scope.login = function(){

  function validate(){
     if($scope.user==null ){
          $scope.mess = "Email id and password are required!";
     }else if(!$scope.user.password || !$scope.user.email){
         if(!$scope.user.password)
         $scope.password = "Password is required";
         if(!$scope.user.email)
         $scope.email = "Email is required";
         return false;
     }

     return true;
   }

   if(validate()){
     TCPService.login($scope.user).then(function(res){
       $scope.mess = "Logged in";
     }).catch(function (error) {
       console.log(error.data);
     });
   }
}
}]);

Network trace1 Network trace2

20
  • It should be like res.redirect('/home.html'); you need to create anothe app.get method and serve html page from there. Commented Dec 27, 2017 at 6:10
  • Do you have a route defined for /home.html? And, is the /users/login request coming from a browser form post or from a Javascript Ajax request? Ajax calls do not change the page the browser is showing at all by themselves. Commented Dec 27, 2017 at 6:11
  • @Sagar - The return in return res.redirect(...) makes no difference at all in that particular route. Commented Dec 27, 2017 at 6:12
  • /users/login coming from here. Its a factory that is being used by the controller. angular.module('httpService',[]). factory('TCPService',['$http',function($http){ return { login : function(user) { return $http.post('/users/login',user); }, create : function(user) { return $http.post('/users', user); } } }]); Commented Dec 27, 2017 at 6:15
  • Possible duplicate of How to make a redirect (301) in Node.js / Express? Commented Dec 27, 2017 at 6:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.