maria
maria

Reputation: 159

route post wont work angularjs / MEAN

Im trying to post a action number from client sided to server sided, and recieve a response from the server, but i do get:

angular.js:8467 POST SITE:3000/posts/test 404 (Not Found)

what is wrong with my code?

server sided index.js

    router.post('/posts/test', auth, function(req, res, next) {


res.json("success");

});

angularjs client file ( under post service):

o.crimePerform = function(post) {
      return $http.post('/posts/test', post, {
    headers: {Authorization: 'Bearer '+auth.getToken()}
  }).success(function(data){
    console.log(data);
    console.log("performed crime!");
  });
}

angularjs client file ( acual action performed)

    $scope.performacrime  = function(action) {
    console.log("performing action number " + action.id);
    posts.crimePerform({crimeaction : action.id});
    $scope.status = action.name + ' loading...';


    $scope.crimeresult = true;


}

response: angular.js:8467 POST WEBSITE:3000/posts/test 404 (Not Found)

EDIT: the response from server is just an example. No matter what the response is, it wont work.

Upvotes: 0

Views: 38

Answers (1)

Wake
Wake

Reputation: 1696

Your problem doesn't appear to be with your angular POST. It's on the server side. Your server side code is erroring out.

I tried for example:

curl -H "Content-Type: application/json" -X POST -d '{"Authorization":"Bearer xyz"}' http://178.62.5.64:3000/posts/test

Which came back with:

<h1>Not Found</h1>
<h2>404</h2>
<pre>Error: Not Found
    at /opt/mean/app.js:48:15
    at Layer.handle [as handle_request] (/opt/mean/node_modules/express/lib/rout
er/layer.js:82:5)
    at trim_prefix (/opt/mean/node_modules/express/lib/router/index.js:270:13)
    at /opt/mean/node_modules/express/lib/router/index.js:237:9
    at Function.proto.process_params (/opt/mean/node_modules/express/lib/router/
index.js:312:12)
    at /opt/mean/node_modules/express/lib/router/index.js:228:12
    at Function.match_layer (/opt/mean/node_modules/express/lib/router/index.js:
295:3)
    at next (/opt/mean/node_modules/express/lib/router/index.js:189:10)
    at /opt/mean/node_modules/express/lib/router/index.js:191:16
    at Function.match_layer (/opt/mean/node_modules/express/lib/router/index.js:
295:3)</pre>

Your server side route is being invoked, and it looks like the error handling is giving the 404 response.

Upvotes: 1

Related Questions