node-restify icon indicating copy to clipboard operation
node-restify copied to clipboard

serveStatic is non-intuitive

Open CodersBrothers opened this issue 11 years ago • 33 comments

KISS: Keep it simple stupid. Please make it this for static files plugins, all people have the same problem, in my ofice 5 persons give me the same as bug!!

if you assign this:

server.get(/\/docs\/public\/?.*/, restify.serveStatic({
  directory: './public'
}));

will mean that if you get the url "domain.com/docs/public" this will be load the content of "public" folder. END. Not at prefix for the entire route path... and your fairy tales... Kiss men, kiss...

CodersBrothers avatar Feb 18 '14 06:02 CodersBrothers

+1

Had to rehack static.js for one application because of this, but would really like to see official solution. I'd suggest to make it as codersbrothers said, or even:

if(typeof opts.directory === 'function') { file = opts.directory(req.path()); }

It's not even important what you do - if it's ok; but please do something

melkior avatar Feb 18 '14 14:02 melkior

On it!

gergelyke avatar Feb 18 '14 16:02 gergelyke

^^

CodersBrothers avatar Feb 18 '14 18:02 CodersBrothers

#526

gergelyke avatar Feb 18 '14 19:02 gergelyke

Which version are you using btw?

gergelyke avatar Feb 23 '14 06:02 gergelyke

Im using 2.6.1, i use alwais the last, in package.json i put "*" :P

CodersBrothers avatar Feb 23 '14 19:02 CodersBrothers

I saw that not its the last version, that 2.6.2 have: #496 static plugin incorrectly handling directories; revert back to 2.6.0 version

This also add something KISS?

CodersBrothers avatar Feb 23 '14 19:02 CodersBrothers

You should never use the asterisk, that is an antipattern! npm install restify --save will save with correct versioning, please use this, not the asterisk

gergelyke avatar Feb 23 '14 19:02 gergelyke

thanks, i will use right now!

CodersBrothers avatar Feb 23 '14 19:02 CodersBrothers

That's great news @gergelyke, I think the behaviour @CodersBrothers describes follows the principle of least surprise.

rprieto avatar Feb 26 '14 22:02 rprieto

Just released the new version, please check it out!

gergelyke avatar Feb 27 '14 07:02 gergelyke

@gergelyke what is the new version you mentioned? I can't seem the find a fix-related commit recently.

rprieto avatar Mar 04 '14 00:03 rprieto

Can you please re-open this issue? It still isn't fixed, the directory property is still being used as a prefix for the full file-path. I would expect that the directory property defines the dir from where the requested filename (not the filepath) is served.

thabemmz avatar Apr 14 '14 13:04 thabemmz

Yes, I agree, the directory property is not intuitive at all - can this be fixed asap please.

forward31 avatar Apr 28 '14 15:04 forward31

Yet still the same? LOL! Yet its so hard enable static files? You are joking?

CodersBrothers avatar May 17 '14 22:05 CodersBrothers

This not was resolved @gergelyke... all people are saying you... why you don't hear?

CodersBrothers avatar May 17 '14 22:05 CodersBrothers

I think restify should not bother with serving static files - it is not its responsibility @mcavage how about removing it in the next major?

gergelyke avatar May 18 '14 07:05 gergelyke

I think the same, restify will be for REST. But if you decide that can serve static, please make this KISS

CodersBrothers avatar May 18 '14 15:05 CodersBrothers

+1 If you have any sort of complex URL scheme (like path based routing), being able to filter out a prefix is really nice.

devonhumes avatar May 27 '14 21:05 devonhumes

Does anyone have a quick fix for this?

cchojnacki avatar Jun 08 '14 17:06 cchojnacki

I think restify should not bother with serving static files

Why? It's not a big addition and it allows restify to also serve a HTML5 client. All my apps do this.

Anyway, nice little hack for virtual subdirectories:

image

naggie avatar Aug 27 '14 21:08 naggie

Another vote for fixing or removing this feature, or at least calling out the behavior more clearly in the docs.

ryanvarick avatar Sep 03 '14 23:09 ryanvarick

As long as it's possible to do this with connect middleware or something, and documented. I think this is a big use case but I agree that wether it's in the scope of restify or not if questionable.

naggie avatar Sep 04 '14 09:09 naggie

Because of CORS letting a REST API also serve a client is useful. Having it as minimal as here is useful. Coding a function to serve static file by yourself is easy though.

aggsol avatar Nov 13 '14 13:11 aggsol

The existing implementation of serveStatic is broken and the example straight out of docs does not work. I have wasted a couple of days due to that, and now switched to using module serve-static.

phanimahesh avatar May 11 '15 12:05 phanimahesh

I agree with most on this thread. Serving static files isn't part of Restify's core competencies. Rather than continue to include a broken static file serving implementation, the existing plugin should be removed, in favor of pointing users to documentation on how to perform the same function.

I'm in favor of spinning the existing plugin out to it's own module and allowing people to include it if they'd like.

micahr avatar Jun 18 '15 05:06 micahr

What @micahr says ^^ +1

Maxwell2022 avatar Sep 15 '15 01:09 Maxwell2022

GETting static files is the epitome of REST! HTML is a media type that uses HATEOAS, so it is the original (true) REST API format. I would love to see restify support this in an easy to use and intuitive way.

figadore avatar Dec 10 '15 21:12 figadore

4 years after and still failing :/

wasa4587 avatar Feb 23 '18 03:02 wasa4587

In case someone have this issue, This worked for me :/

server.get(/\/?.(js|css|png|jpg)/, restify.plugins.serveStatic({
    directory: './public',
    default: '/index.html'
}));

server.get(/\/?.*/, restify.plugins.serveStatic({
    directory: './public',
    file: '/index.html'
}));

wasa4587 avatar Feb 23 '18 18:02 wasa4587