1

A https POST from an angular factory to a node/express route works but Chrome is giving me a 404 error. Here is the code snippet from the angular factory:

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

And here is the route

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;

I have this cors code in index.js:

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

This is the Chrome tools output:

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".

and the headers for the 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:*

and the headers for the 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:*

Can anyone enlighten me how I can fix this error. Like I said the post works but red error messages cause a disturbance in the force.

meanstacky
  • 387
  • 1
  • 8
  • 18

0 Answers0