Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I inspect the set of Express.js middleware that is being used?

Backstory: I'm trying to debug an issue in one piece of middleware that I think is coming from other piece. But, I'm not sure. So anyway, I would like to be able to check what middleware is actually being called, because I'm not sure of the ordering.

Is it possible to inspect the set of middleware that is currently being used?

I have tried to find any piece of internal state where Express might be storing the middleware, but I was not able to.

like image 628
hrdwdmrbl Avatar asked May 22 '15 18:05

hrdwdmrbl


People also ask

Which of the following is a middleware in Express JS?

ExpressJS Online Training Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application's request-response cycle.

What does middleware in Express access?

Middleware literally means anything you put in the middle of one layer of the software and another. Express middleware are functions that execute during the lifecycle of a request to the Express server. Each middleware has access to the HTTP request and response for each route (or path) it's attached to.

Which function is used to associate middleware for Express js action?

An Express-based application is a series of middleware function calls. Advantages of using middleware: Middleware can process request objects multiple times before the server works for that request. Middleware can be used to add logging and authentication functionality.

What does the Express JSON () middleware do and when would you need it?

json() is a built-in middleware function in Express. This method is used to parse the incoming requests with JSON payloads and is based upon the bodyparser. This method returns the middleware that only parses JSON and only looks at the requests where the content-type header matches the type option.


1 Answers

How middleware works:

The middlewares are generally used to transform a request or response object, before it reaches to other middlewares.

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

Example,

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

the order is

  1. compression,
  2. passport,
  3. bodyParser,
  4. cookieParser

(plus I think your bodyParser and cookieParser middlewares should be before the other middlewares like passport).

That is the reason why the error handling middlewares are kept at last, so that if it reaches them, they give an error response.

So basically, request drips down the middlewares until one of them says that it does not want it to go any further(get, post methods are such middlewares, that stop the request).

Now, the debugging part:

You may not be able to inspect the middleware properly internally, but you can check whether middleware has worked properly by inserting your own custom middleware in between and then put a breakpoint on it.

Let's say you want is to debug what happened to your request after the bodyParser middlewares does it tasks, you can do is put your custom middleware in between and check the request and response whether they are modified properly or not. how you do this is by following example.

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 way in which you move this custom debugger in between the middlewares, until you figure out which one is giving faulty outputs.

Also, if you want to check the middlewares functionality, you can look at the documentation of those middlewares. They are quite good.

Check by playing with your custom middleware.

like image 81
Kop4lyf Avatar answered Nov 15 '22 09:11

Kop4lyf