开发者

expressjs bind all routes except 2 folders?

开发者 https://www.devze.com 2023-03-18 00:02 出处:网络
In expressjs, how do I bind a route to all urls except /static and /fail For example, it sound bind to:

In expressjs, how do I bind a route to all urls except /static and /fail

For example, it sound bind to:

/users
/books
/books/anything
开发者_如何学C

but not bind to

/static
/fail
/fail/anything
/static/anything


If you are saying you want to create one route for everything but /static* then here is the command for creating the GET route:

app.get(/^((?!\/static).)*$/, function(req, res){

    //Do your thing in here...
});


My question was just slightly different and this was the best question/answer combo I found so I wanted to share my solution stolen from Clint's answer. If you need like me to restrict from a list of couple of routes like /static and /fail, the following worked for me:

app.get(/^(?!(\/static|\/fail)).*$/, function(req, res, next){

  //Do your thing in here...
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号