Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: dir index #135

Closed
simone-sanfratello opened this issue Jun 24, 2020 · 5 comments
Closed

Proposal: dir index #135

simone-sanfratello opened this issue Jun 24, 2020 · 5 comments

Comments

@simone-sanfratello
Copy link
Contributor

simone-sanfratello commented Jun 24, 2020

🚀 Feature Proposal

I'd like to implement a feature to get the "index" of the directory, if the request is the dir root or "index" or "index.html" - all of that optional and configurable. Output format can be json or html; for html a string for template is required.
Next improvement could optionally add files stats to output

Motivation

Can be useful to have an index for dir navigation or to quickly expose files

Example

A draft example could be

json

fastify.register(require('fastify-static'), {
  root: path.join(__dirname, 'public'),
  prefix: '/public/',
  index: { // optional
    format: 'json', // optional, default json (?), can be both ?
    names: ['/index', '/index.json', '/'] // optional, default '/'
  }
})

request
GET .../public/images or GET .../public/images/index.json

response

{
  "dirs": ["house", "office"],
  "files": ["bed.jpg", "kitchen.png"]
}

html

fastify.register(require('fastify-static'), {
  root: path.join(__dirname, 'public'),
  prefix: '/public/',
  index: { 
    format: 'html',
    template: '<html> ... TDB ... </html>',
    names: ['/index', '/index.html', '/'] // optional, default '/'
  }
})

request
GET .../public/images or GET .../public/images/index.html

response

<html>

  DIRS
  <a href="/public/house">house</a>
  <a href="/public/office">office</a>

  FILES
  <a href="/public/bed.jpg" target="_blank">bed.jpg</a>
  <a href="/public/kitchen.jpg" target="_blank">kitchen.jpg</a>
</html>
@mcollina
Copy link
Member

Would you like to send a Pull Request to implement this feature? Remember to add unit tests.

@simone-sanfratello
Copy link
Contributor Author

Sure, I just wanted to check if the feature makes sense

@mcollina
Copy link
Member

I thought about implementing it for a while! :)

@mcollina
Copy link
Member

mcollina commented Jun 25, 2020

Put it behind an option.

@simone-sanfratello
Copy link
Contributor Author

simone-sanfratello commented Jun 29, 2020

I made the PR #137

I changed some names from this proposal, the index option name was already taken.
I introduced the lib dir because I'm not confortable with a single file module, but if this is not consistent, I can put that inside index.js.

No need to say, feel free to change anything you like, but please note I get 100% coverage on lib/dirList.js and only 2 lines are missing in index.js from 100% code coverage :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants