jfhbrook / node-ecstatic

A static file server middleware that works with core http, express or on the CLI!
https://github.com/jfhbrook/node-ecstatic
MIT License
975 stars 194 forks source link

Serve multiple directories #192

Open viveleroi opened 7 years ago

viveleroi commented 7 years ago

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?

fenwick67 commented 7 years ago

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 }));
viveleroi commented 7 years ago

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

viveleroi commented 7 years ago

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

jfhbrook commented 7 years ago

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?

viveleroi commented 7 years ago

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.

jfhbrook commented 7 years ago

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.