使用AngularJS$http.post的Express server status 404错误

使用AngularJS$http.post的Express server status 404错误,angularjs,node.js,express,Angularjs,Node.js,Express,因此,我为我的express服务器和我的angular前端设置了单独的端口,这是我第一次使用yeoman和grunt。这也是我第一次尝试使用这种客户机/服务器结构的web项目,我遇到了麻烦 我在前端的localhost:9000/signup下有一个注册表单,当我单击submit时,我会返回以下内容 {firstname: "test", lastname: "test", email: "test", password1: "test", password2: "test"} angular.

因此,我为我的express服务器和我的angular前端设置了单独的端口,这是我第一次使用yeoman和grunt。这也是我第一次尝试使用这种客户机/服务器结构的web项目,我遇到了麻烦

我在前端的localhost:9000/signup下有一个注册表单,当我单击submit时,我会返回以下内容

{firstname: "test", lastname: "test", email: "test", password1: "test", password2: "test"}
angular.js:12759 POST http://localhost:9000/signup 404 (Not Found)

(anonymous) @ angular.js:12759
sendReq @ angular.js:12492
serverRequest @ angular.js:12244
processQueue @ angular.js:17051
(anonymous) @ angular.js:17095
$digest @ angular.js:18233
$apply @ angular.js:18531
(anonymous) @ angular.js:27346
dispatch @ jquery.js:5206
elemData.handle @ jquery.js:5014
angular.js:14700 Possibly unhandled rejection: {"data":"Cannot POST     /signup\n","status":404,"config":
{"method":"POST","transformRequest":        
[null],"transformResponse:[null],"jsonpCallbackParam":"callback","url":"/signup","data":
{"firstname":"test","lastname":"test","email":"test","password1":"test",
"password2":"test"},"headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json;charset=utf-8"}}
,"statusText":"Not Found","xhrStatus":"complete"}
第一行是我将表单的结果打印回浏览器控制台。第二个是404开/注册,我不确定这是特快路线还是客户/注册

这是我的signup.js,位于express的/routes文件夹中

// Include Express
var express = require('express');
// Initialize the Router
var router = express.Router();

// Setup the Route
router.post('/', function (req, res) {

// show the request body in the command line
console.log(req.body);

// return a json response to angular
res.json({
    'msg': 'success!'
});
});

// Expose the module
module.exports = router;
我已经将其包含在express中的app.js文件中

var signup = require('./routes/signup');
...
app.use('/signup', signup);
Abd最后是我的signup.js文件夹,它是/signup表单的控制器

'use strict';

angular.module('clientApp') // make sure this is set to whatever it is in your client/scripts/app.js
  .controller('SignupCtrl', function ($scope, $http) { // note the added $http depedency

    // Here we're creating some local references
    // so that we don't have to type $scope every
    // damn time
    var user,
        signup;

    // Here we're creating a scope for our Signup page.
    // This will hold our data and methods for this page.
    $scope.signup = signup = {};

    // In our signup.html, we'll be using the ng-model
    // attribute to populate this object.
    signup.user = user = {};

    // This is our method that will post to our server.
    signup.submit = function () {

      // make sure all fields are filled out...
      // aren't you glad you're not typing out
      // $scope.signup.user.firstname everytime now??
      if (
        !user.firstname ||
        !user.lastname ||
        !user.email ||
        !user.password1 ||
        !user.password2
      ) {
        alert('Please fill out all form fields.');
        return false;
      }

      // make sure the passwords match match
      if (user.password1 !== user.password2) {
        alert('Your passwords must match.');
        return false;
      }

      // Just so we can confirm that the bindings are working
      console.log(user);


    $http.post('/signup', user)
    .then(function(response) {
      console.log(response.data);
  });
}
  });

我被正式难倒了,希望能有一些见解-非常感谢您的帮助。

问题是您正在向localhost:9000/signup发布,这是前端,但前端不支持向该路线发布方法。您需要将请求发送到localhost:{YOUR_EXPRESS_SERVER_PORT}/signup

改变这个
$http.post('/signup',user)

$http.post('http://localhost:{YOUR_EXPRESS_SERVER_PORT}/signup',user)

在哪个端口上运行EXPRESS?