3

I am trying use restify to serve all paths that don't begin with /api from a directory containing static files.

var restify = require('restify');

var server = restify.createServer();                                                                           
server.get(/^\/(?!api)/, restify.serveStatic({                                                                   
    directory: './static'                                                                                        
}));                                                                                                             

server.listen(8080, function() {                                                                                 
    console.log('%s listening at %s', server.name, server.url);                                                
}); 

But, when I attempt going to, say, http://0.0.0.0:8080/index.html, I get:

{"code":"InternalError","message":"Invalid regular expression: /^/(?!a/: Unterminated group"}

I am doubly confused because:

 $ node
> var e = /^\/(?!api)/;
undefined
> e.test('/api/v1');
false
> e.test('/index.html');
true
Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
  • it's because u are using ?! which is basically checking for the negation of what you actually desire. `/^\/(api)/.test('/api/v1')` this should work for you I believe. And if it's just a test that u are performing the you don't even need those "()" round brackets a simple `/^\/api\//` should do. – Sushil Feb 22 '14 at 19:49
  • @Sushil: I want the opposite of that. – Dmitry Minkovsky Feb 22 '14 at 19:52
  • The regex looks good, it will match `/foo` but not `/api/foo` – mgamba Feb 22 '14 at 19:53
  • ah, ok I thought you wanted to filter the URL's containing `/api/` – Sushil Feb 22 '14 at 19:53
  • That's right: as I wrote in my question, I want all paths not matching `/api` to be served from a static folder. – Dmitry Minkovsky Feb 22 '14 at 19:55
  • It looks like it might be misinterpreted ... `/^/(?!a/` in the error message is not the original `/^\/(?!api)/` – mgamba Feb 22 '14 at 19:56
  • Yeah... what's with that? I've tried wrapping `RegExp`, and all that. I don't get it. – Dmitry Minkovsky Feb 22 '14 at 19:57
  • Do you get a stack trace? Since you are getting it when you request rather than when you start the server, it means `/^\/(?!api)/` is perfectly valid, as you have seen. There must be something in Restify that processes the regex that is breaking. Maybe it doesn't expect `?!` – loganfsmyth Feb 22 '14 at 20:01
  • I'm about to run this with `--debug` and pause on all exceptions to find out what it's doing. This has killed an hour of my life, "lol". – Dmitry Minkovsky Feb 22 '14 at 20:02
  • @mgamba: yeah, I saw http://stackoverflow.com/a/406408/741970 and tried that. no dice. – Dmitry Minkovsky Feb 22 '14 at 20:05
  • What's weird is that I also tried `/^\/[^a][^p][^i]/` and that's also messing up. I think something wrong in restify. – Dmitry Minkovsky Feb 22 '14 at 20:05
  • Wow... the offending restify code literally has this line: `route = route.substring(1, route.length-4);`. Just trims off the last 4 chars of my regexp. ?!? – Dmitry Minkovsky Feb 22 '14 at 20:28

1 Answers1

3

The static files plugin was broken at the time of this posting. See my debug at https://github.com/mcavage/node-restify/issues/537.

Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160