Igal
Igal

Reputation: 135

mean.js angular - express routing

I'm new to the MEAN stack and I'm having some trouble with routing...

I have a module called "applications". the APIs i want on the server side are: get: http://localhost:3000/api/applications/(_appid) getByMakeathonId: http://localhost:3000/api/applications/makeathons/(_mkid)

Applications Service

function ApplicationsService($resource) {
    return $resource('api/applications/:path/:applicationId', {
      path: '@path',
      applicationId: '@id'
    }, {
      get: {
        method: 'GET',
        params: {
          path: '',
          applicationId: '@_id'
        }
      },
      getByMakeathonId: {
        method: 'GET',
        params: {
          path: 'makeathon',
          applicationId: '@_id'
        }        
      },          
      update: {
        method: 'PUT'
      }
    });

Server Routing

  app.route('/api/applications').post(applications.create);
  app.route('/api/applications').all(applicationsPolicy.isAllowed)
    .get(applications.list);                   
  app.route('/api/applications/makeathon/:makeathonId').all(applicationsPolicy.isA llowed)
      .get(applications.applicationByMakeathonID);

1) what I'm getting when I call $save and the object and the save is successful, there is a call for .get, and the request URL is: http://localhost:3000/api/applications//56f15736073083e00e86e170 (404 not found) the problem here of course is the extra '/' - How do I get rid of it.

2) when I call getByMakeathonId, the request url is: http://localhost:3000/api/applications/makeathon?id=56e979f1c6687c082ef52656 400 (Bad Request)

I do I configure so that I'll get the two requests that I want?

10x!

Upvotes: 0

Views: 84

Answers (1)

Bennett Adams
Bennett Adams

Reputation: 1818

You are getting the repeated // in your request url because you have declared that there will be a :path in your applications resource, and you are providing an empty string to interpolate there.

As $resource is meant to provide RESTful interaction with your API, I think the most appropriate approach would be to have separate $resources to deal with applications and makeathons. Something like this:

For applications:

function ApplicationsService($resource) {
  return $resource('api/applications/:applicationId', {
    applicationId: '@id'
  }, {
    update: {
      method: 'PUT'
    }
  });
}

For makeathons:

function MakeathonsService($resource) {
  return $resource('api/applications/makeathons/:makeathonId', {
     makeathonId: '@id'
    }
  });
} 

/** your server route would then be updated to
 * app.route('/api/applications/makeathons/:makeathonId')...
 */

Upvotes: 1

Related Questions