Tuesday, 23 May 2017

app.put('url', middleware) doesn't run the middleware, Express NodeJs

I have some middleware that validates all put and post requests to have proper parameters. My mocha tests fail for my update routes, and after debugging I've discerned its because my router doesn't go through my validation middleware for update requests, though it works fine for all posts and I mount the middleware for both at the same place and time, in the same way. Can anyone spot the issue? Below you'll find my test code, validation middleware, and where I mount routes.

In server.js:

//400 middleware

//this mounts the middleware succesfully
app.post('/:table', helper.validateParameters);

//this doesn't
app.put('/:table', helper.validateParameters);

In my test file:

it('should return a 400 error if no parameters are passed', (done) =>{

//this runs through the validation middleware

  request(app)
  .post('/item/')
  .set('Authorization', testSuite.tokenHeader)
  .expect(400)
  .end(done);
});

...

it('should return a 400 error if no parameters are passed', (done) => {

//this doesn't

  request(app)
  .put(testSuite.realItemUrl)
  .set("Authorization", testSuite.tokenHeader)
  .expect(400)
  .end(done);
});

In helper.js:

let validateParameters = (req, res, next) => {

  //The route handler never enters here when handling Item.put requests

  if (!req.body.params || (req.body.params && !checkProperties(req.params.table, req.body.params))) {
    //winston.log?
    return next(ErrorTypes.badRequest());
  }
  else {
    return next();
  }

 }



via David Tamrazov

No comments:

Post a Comment