不能发表角度表达形式

时间:2016-10-29 12:11:43

标签: javascript angularjs node.js express passport.js

我对angularJS中的帖子请求有问题要表达。 在客户端,有一个HTML表单定义:

angular.module('app')
  .controller('LoginCtrl', ['$scope', '$state', '$http', function($scope, $state, $http) {

    $scope.login= function(username, password) {
      console.log('Login');
      $http.post('/login', {username:username, password:password}).then(function(response) {
        if(response.data.success) {
          console.log('logged in');
          $state.go('home');
        }
        else {
          console.log('failed to log in');
        }
      });

    };

    $scope.user = {
      username: '',
      password: ''
    };
}]);

和Angular控制器中的post方法:

var express = require('express');
var router = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var User = require('../models/user');

// Login
router.get('/login', function(req, res){
    res.render("login");
});


passport.use('local', new LocalStrategy(
  function(username, password, done) {
   User.getUserByUsername(username, function(err, user){
    if(err) throw err;
    if(!user){
        return done(null, false, {message: 'Unknown User'});
    }

    User.comparePassword(password, user.password, function(err, isMatch){
        if(err) throw err;
        if(isMatch){
            return done(null, user);
        } else {
            return done(null, false, {message: 'Invalid password'});
        }
    });
   });
  }));

passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.getUserById(id, function(err, user) {
    done(err, user);
  });
});

router.post('/login',
  passport.authenticate('local', {successRedirect:'/', failureRedirect:'/login',failureFlash: true}),
  function(req, res) {
    res.redirect('/');
  });

module.exports = router;

在路由的服务器端,有users.js文件,其中定义了中间件:

POST http://localhost:3000/login 404 (Not Found)

当我点击提交按钮时,控制台中会显示错误消息:.navbar-header { min-height: 150px; } .navbar { background: transparent; min-height: 113px; top: 0px; font-size: 18px; width: auto; border: none; padding: 0px 0px; } .navbar-default .navbar-nav > li > a { color: #fff; } .navbar-brand { padding: 0 15px; height: 113px; } @media (min-width: 768px) { .navbar-nav { position: relative; right: -50%; } .navbar-nav > li { position: relative; left: -50%; } .navbar-nav > li a { line-height: 113px; vertical-align: middle; padding: 0 24px; } } @media (min-width: 992px) { .navbar-nav > li a { padding: 0 48px; } } @media (min-width: 1200px) { .navbar-nav > li a { padding: 0 68px; } }

有人知道问题出在哪里吗?

1 个答案:

答案 0 :(得分:0)

您缺少登录post。你需要这样的东西:

// Login
router.post('/login', function(req, res){
    // validate user credentials and login user
});