快速JS
我使用ExpressJS 4.x的开发REST Web服务器。我想要做的就是写捕获所有传入的请求,除了开头的那些“/ API / ......”中间件
我可以使用哪种正则表达式?
//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)=>{
......
})
回答如下:
根据这一SO answer,您可以使用negative look ahead(他们在Javascript提供):
app.use(/\/((?!api).)*/, app_lookup);
正如你所看到的,正则表达式没有用引号括起来。
快速JS
我使用ExpressJS 4.x的开发REST Web服务器。我想要做的就是写捕获所有传入的请求,除了开头的那些“/ API / ......”中间件
我可以使用哪种正则表达式?
//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)=>{
......
})
回答如下:
根据这一SO answer,您可以使用negative look ahead(他们在Javascript提供):
app.use(/\/((?!api).)*/, app_lookup);
正如你所看到的,正则表达式没有用引号括起来。