How can I test the Express.js middleware suite that is in use?

Backstory: I am trying to debug a problem in one piece of middleware that I think comes from another piece. But I'm not sure. Anyway, I would like to be able to check what is actually called middleware because I am not sure about the order.

Is it possible to test the set of middleware that is currently in use?

I tried to find any part of the internal state where Express can store middleware, but I couldn't.

+3


source to share


2 answers


You cannot see what exactly the "middleware" is, but you can check all registered handlers.

In expression 4:

// Routes registered on app object
app._router.stack

// Routes registered on a router object
router.stack

      



Great for testing / debugging, perhaps not a good idea for programming against any variable preceded by an underscore.

Maybe this will help: http://thejackalofjavascript.com/list-all-rest-endpoints/

+2


source


How middleware works:

Medium means are usually used to transform a request or response object before it reaches other intermediaries.

If you are concerned about the order in which the proxies are called, express the middleware calls in the order in which they are defined.

Example

app.use(compression());
app.use(passport.initialize());
app.use(bodyParser());
app.use(cookieParser());

      

order

  • compression,
  • the passport,
  • bodyParser,
  • cookieParser

(plus I think your body and salesperson marketing partners should be in front of other averages like passport).

It is for this reason that error handling facilities are finally supported, so if they get there, they will return an error response.



So basically, the request trickles down the middle sides until one of them says he doesn't want it to go any further (get, post methods are the kind of intermediaries that stop the request).

Now the debugging part :

You may not be able to test the middleware internally, but you can test if the middleware is working as expected by adding your own middleware in between and then placing a breakpoint on it.

Suppose you want to debug what happened to your request, after the bodyParser proxies do the tasks, you can do this by placing your middleware in between and checking the request and response to see if they have changed correctly or not. how do you do it by following the example below.

Example

app.use(bodyParser());

//custom middleware to inspect which middleware is not  working properly
app.use(function(req,res,next){
   console.log("check something!"); //do something here/put a breakpoint
   next(); 
   /*this function is the third parameter 
   and need to be called by the middleware 
   to proceed the request to the  next middleware,
   if your don't write this line, your reqest will just stop here.
   app.get/app.post like methods does not call next*/
});

app.use(cookieParser());

      

This is one of the ways that you move this custom debugger between averages until you figure out which one is giving faulty outputs.

Also, if you want to test the functionality of middlewares, you can look at the documentation of these middlewares. They are not bad.

Check it out by playing with your special middleware.

+2


source







All Articles