[Fixed] Express.use with empty path

Issue

I got some code from a tutorial where express is used to make a NodeJS Proxy, they eventually use express.use with an empty string for the path. I can’t find what the empty string will do is it the same as ‘/’?

The code looks like this :

app.use('', (req, res, next) => {
  if(req.headers.authorization.includes('Bearer')){
    next();
  } else {
    res.sendStatus(403);
  }
})

Solution

It is not the same as /. It means that middleware will be used every time a request hits your server.

You can remove it for brevity.

app.use((req, res, next) => {
  if(req.headers.authorization.includes('Bearer')){
    next();
  } else {
    res.sendStatus(403);
  }
});

Leave a Reply

(*) Required, Your email will not be published