Home > Blockchain >  The worst JavaScript bug on the planet ever exposed, ExpressJs middlewares gets executed on app star
The worst JavaScript bug on the planet ever exposed, ExpressJs middlewares gets executed on app star

Time:07-16

I have the following code:

const app = require("express")();

const middleware = (obj) => (req, res, next) => {
  next();
};

app.get(
  "/users",
  middleware({
    id: req.login.id,
  })
);

app.listen(9999, () => console.log("test running on port 9999"));


When I run the app it gives the following error:

ReferenceError: req is not defined

I know that req is not defined in the middleware, but, my question is, who runs the middleware before even sending any request (on app launch).

why it gets fired? who invokes it? and how to prevent that?

If you have this code, for example, no error will be thrown:

const y = (propName) => console.log(propName);
const x = () => y({ req: req.login.id });

because x is never called. but in my express app, why does it read req.login.id on app launch?

CodePudding user response:

As i understand, you've got an error on line 10:

app.get(                // 7
  "/users",             // 8
  middleware({          // 9
    id: req.login.id,   // 10
  })                    // 11
);                      // 12

There's a difference between your code and your example: In your code, you've executed app.get(...) function, so the get(...) function have their own arguments. The second argument is middleware(...), which is also a function. middleware(...) function don't have to be executed, and you're right about that, but the middleware(...) is an argument as well (for the parent function), but as a function, it should be defined with their own arguments. It means that you can't put there whatever you want inside of middleware(...) function's brackets as the arguments. You have to put there only defined stuff. Please don't mix the javascript command with the function definition. So the diffirence between your code and your given example that in your example you just have some not defined stuff in the body of the function, but not as an argument. You can be sure about that, when you will write your same example in another way, which is the same:

const y = (propName) => {   // 1
  console.log(propName)     // 2
};                          // 3
const x = () => {           // 4
  y({ req: req.login.id })  // 5
};                          // 6

As you can see in the 5th line, you only have some stuff in the body, that's why it never been executed, but in you code you had some stuff which was executed.

P.S. I saw some answer, so I thought it could be useful to write here an alternative code as well. You can modify your code like this, where the req is used inside of middleware's body:

const app = require("express")();

const middleware = (obj) => (req, res, next) => {
    // use req.login.id here...
    next();
};

app.get("/users", middleware);

app.listen(9999, () => console.log("test running on port 9999"));

CodePudding user response:

Because you're calling a function to define the middleware. You should use it as below.

const app = require("express")();

const middleware = (obj) => (req, res, next) => {
const {login:{id}} = req.params;
if(id){
// Things to do...
}
next();
};

app.get(
  "/users",
  middleware
);

  • Related