简体   繁体   中英

How do I pass in parameters to the express.js server side routing for a MEAN.js package.

I am a beginner working on a project structured by mean.js. I am still trying to figure out how the whole project struct works and I am a little stuck.

Currently, I have two crud modules. A project module and an application module. Users should be able to apply to projects. Under a certain project, I should be able to view all the applications and then accept/reject them.

I added a custom method onto the angular $resource

function ProjectApplicationsService($resource) {
    return $resource('api/projects/:projectId/applications', { projectId: '@_id' }, {
      accept: {
        method: 'PUT'
      },
      reject: {
        method: 'PUT'
      }
    });

  }

Once I click a button corresponding to a certain application, it will trigger this function and pass in the application ID as a parameter.

function acceptApp(applicationID){
      vm.application._id = vm.project._id;
      vm.application.app_id = applicationID;
      vm.application.$accept(successCallback, errorCallback);
      function successCallback(res) {
          console.log("success");
      }

      function errorCallback(res) {
        vm.error = res.data.message;
        console.log(vm.error);
      }
    }

This should make an API call to express and here is the routing for that

  app.route('/api/projects/:projectId/applications')
    .get(users.requiresLogin, projects.hasAuthorization, projects.getApplications)
    .put(users.requiresLogin, projects.hasAuthorization, projects.updateApplication)
    .delete(users.requiresLogin, projects.hasAuthorization, projects.deleteApplication);

Essentially, I want to end up calling updateApplication but how do I get the application ID within this method?

Assuming you're going to upload something to the server which will trigger your put route - which contains the function you want to call.

You need to structure your handles like so:

function(request,response,next){ // usually abbreviated to `req,res`
    handleData();
    next(); // move onto the next function;
}

The parameters are easy to remember by, request comes before response, and then next - You only need next when you want to move onto another handler.

If you don't call next() the route will stop - you'll probably want to call next once authorization succeeds.

data is held in the requests body, so you can access it using request.body

Note: remember to respond to your user - with something like res.send("success") , or they'll time out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM