最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - expressjs bind all routes except 2 folders? - Stack Overflow

programmeradmin0浏览0评论

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

but not bind to

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

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

but not bind to

/static
/fail
/fail/anything
/static/anything
Share Improve this question edited Jun 19, 2017 at 12:27 laser 1,37613 silver badges14 bronze badges asked Jul 5, 2011 at 16:59 HarryHarry 55.1k76 gold badges187 silver badges270 bronze badges 5
  • Sounds like bad design. Just bind to users, books, etc individually – Raynos Commented Jul 5, 2011 at 18:16
  • @Raynos routing happens on the client side. – Harry Commented Jul 5, 2011 at 18:27
  • no routing happens on the server. express runs on the server. – Raynos Commented Jul 5, 2011 at 18:35
  • @Raynos express serves one document to all. Actual routing happens on the clientside with backbone.js and html5 pushstate. – Harry Commented Jul 5, 2011 at 18:58
  • should have mentioned that in the question ;) – Raynos Commented Jul 5, 2011 at 19:11
Add a ment  | 

2 Answers 2

Reset to default 8

If you are saying you want to create one route for everything but /static* then here is the mand 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 bo 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)

  1. 暂无评论