2016-03-22 8 views
0

Ich bin neu in der MEAN-Stack und ich habe einige Probleme mit dem Routing ...mean.js eckig - Express-Routing

Ich habe ein Modul namens "Anwendungen". die APIs i auf der Server-Seite wollen, sind: get: http://localhost:3000/api/applications/(_appid) getByMakeathonId: http://localhost:3000/api/applications/makeathons/(_mkid)

Anwendungen 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), was ich Ich bekomme, wenn ich $ save anrufe und das Objekt und die Speicherung erfolgreich ist ul, es gibt einen Aufruf für .get, und die Anfrage URL ist: http://localhost:3000/api/applications//56f15736073083e00e86e170 (404 nicht gefunden) Das Problem hier ist natürlich das Extra '/' - Wie werde ich es loswerden.

2), wenn ich getByMakeathonId nennen, die Anforderungs-URL ist: http://localhost:3000/api/applications/makeathon?id=56e979f1c6687c082ef52656 400 (Bad Request)

ich mich so konfiguriere, dass ich die beiden Anfragen bekommen, was ich will?

10x!

Antwort

1

Sie erhalten die wiederholte // in Ihrer Anfrage-URL, weil Sie in Ihrer Anwendungsressource ein :path deklariert haben, und Sie eine leere Zeichenfolge bereitstellen, um dort zu interpolieren.

Da $resource RESTful Interaktion mit Ihrer API bieten soll, denke ich, der am besten geeignete Ansatz wäre separate $resource s, um mit Anwendungen und makeathons umzugehen. Etwas wie folgt aus:

Für Anwendungen:

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

Für 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')... 
*/ 
+0

Dies hilft gelöst mir ein anderes Problem – Sam