使用express重定向不在node.js中工作

时间:2017-12-27 06:02:04

标签: node.js express

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 + '!');
    });
});

重定向应在发布请求后重定向到新页面home.html但不起作用。不知道我在这里做错了什么。在重定向之前,我正在从我在此代码中删除的db中读取用户详细信息。 的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>

控制器:

使用工厂服务:

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);
  }
}
}]);

控制器:

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

0 个答案:

没有答案