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

Serve multiple directories

Open viveleroi opened this issue 7 years ago • 6 comments

I'd like to serve static files in two directories - one with my html, one with some compiled css that gets distributed with an npm package. I don't see a way to do this?

viveleroi avatar Apr 10 '17 20:04 viveleroi

If you are using Express you should be able to just install two middlewares, one for each directory you want to serve. It will fall through the first middleware if the file isn't found in that directory, and it will move down the middleware chain.

app.use(ecstatic({ root: __dirname + '/compiled-css', handleError: false }));
app.use(ecstatic({ root: __dirname + '/public', handleError: false }));

fenwick67 avatar May 15 '17 14:05 fenwick67

Thanks, but we specifically wanted a light-weight http server and chose not to use express.

viveleroi avatar May 15 '17 14:05 viveleroi

Thanks, but we specifically wanted a light-weight http server and chose not to use express.

viveleroi avatar May 15 '17 14:05 viveleroi

You can do the same trick, roughly, but using stock http, something like:

http.createServer((req, res) => {
  ecstaticFirstDir(req, res, (err) => {
    if (shouldFail(res, err)) {
      return fail(res, error);
    }

    ecstaticSecondDir(req, res);
  });
})

There is no way to do this from the CLI, but if you're doing that anyway...have you considered nginx?

jfhbrook avatar May 15 '17 15:05 jfhbrook

I can try that, though seems trivial for the package to add support for multiple dirs. This has to be purely-node driven based on our use case.

viveleroi avatar May 15 '17 15:05 viveleroi

Adding support for multiple dirs in the middleware itself is probably harder than it sounds, and not a road I want to go down, especially considering more far-reaching work like what #183 entails.

Adding this to the cli might be doable...if you can convince me that your use case is common. I tend to tell people to use nginx for general purpose non-development static file hosting, since that project is much more industrial grade, properly benchmarked, has fewer bugs etc.

jfhbrook avatar May 15 '17 15:05 jfhbrook