0

I'm using ExpressJS 4.x to develop a REST web server. What I'm trying to do is to write a middleware that catches all the incoming requests, except the ones that begin with "/api/..."

Which regular expression can I use?

//This middleware catches everything except the requests addressed to "/api/..."
app.use('<regular_expression>',express.static('public'));

//Not intercepted by the middleware
app.get("/api/foo1",function(req,res)=>{
    ......
})

//Not intercepted by the middleware
app.get("/api/foo2/bar",function(req,res)=>{
    ......
})
Leonardo Carraro
  • 1,532
  • 1
  • 11
  • 24

1 Answers1

2

According to this SO answer, you can use negative look ahead (they are available in Javascript):

app.use(/\/((?!api).)*/, app_lookup);

As you can see, the regular expression is not surrounded by quotes.

Amessihel
  • 5,891
  • 3
  • 16
  • 40
  • 1
    This is the exact regular expression I'm using in production. So I can confirm this works too. – Wyck Feb 08 '19 at 19:45