https POST工作,但令人困惑地显示404错误

时间:2016-10-16 22:57:00

标签: angularjs

从角度工厂到节点/快速路线的https POST工作正常但Chrome正在给我404错误。 以下是角度工厂的代码片段:

userFactory.create = function(userid) {
        return $http.post('https://localhost:3443/api/users/' + userid);
    };

这是路线

router.post('/:userid', function(req, res, done){
    User.findOne({ userid: req.params.userid }, function(err, user) {
      if(err) {
        console.log(err);
      }
      if (!err && user !== null) {
        done(null, user);
      } else {
        user = new User({
          userid: req.params.userid
        });
        user.save(function(err) {
          if(err) {
            console.log(err); 
          } else {
            console.log("saving user ...");
            done(null, user);
          }
        });
      }
    });          
});

module.exports = router;

我在index.js中有这个cors代码:

app.use(function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
  next();
});

这是Chrome工具输出:

POST https://localhost:3443/api/users/346455889030594 404 (Not Found) :3443/api/users/346455889030594:1

angular.js:12011 XHR finished loading: POST "https://localhost:3443/api/users/346455889030594".

以及204的标题:

Request URL:https://localhost:3443/api/users/346455889030594
Request Method:OPTIONS
Status Code:204 No Content
Remote Address:[::1]:3443
Response Headers
view source
Access-Control-Allow-Headers:authorization
Access-Control-Allow-Methods:GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Origin:*

以及404的标题:

Request URL:https://localhost:3443/api/users/346455889030594
Request Method:POST
Status Code:404 Not Found
Remote Address:[::1]:3443
Response Headers
view source
Access-Control-Allow-Headers:X-Requested-With,content-type, Authorization
Access-Control-Allow-Methods:GET, POST
Access-Control-Allow-Origin:*

任何人都可以告诉我如何修复此错误。就像我说的那样,这个帖子有效,但是红色错误信息会导致力量受到干扰。

0 个答案:

没有答案