Express JS-正则表达式-除

时间:2019-02-08 19:17:31

标签: node.js regex express

我正在使用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)=>{
    ......
})

1 个答案:

答案 0 :(得分:1)

根据此SO answer,您可以使用negative look ahead(它们在Javascript中可用):

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

如您所见,正则表达式没有用引号引起来。

相关问题