Roger
Roger

Reputation: 23

Return list of files in a server as a json or text instead of the deafult "Index of" html page

I'm setting up a new server to host files. I need suggestions on how to return a JSON or text with the file names when i perform a GET request to that particular location. I have not built an API service and would want to keep this as simple as possible.

OS - Ubuntu 18.04 Web Server used - NGINX

Here are the steps i have done -

Have added this location block location /docs { alias /home/john/docs/; autoindex on; } When I visit the page , as expected "Index of" is displayed and the list of files which can be downloaded is visible. I would want to know if there is a way where i can return just the file names i.e I would want to receive a JSON or a list of all the files available instead of this page . GET request to /docs can return a text or JSON . Currently i obtain this html

<html>
  <head><title>Index of /docs/</title></head>
  <body bgcolor="white">
    <h1>Index of /docs//</h1>
    <hr>
      <pre>
        <a href="../">../</a>
        <a href="v1.pdf">v1.pdf</a>  17-Aug-2019 06:24             1223345
        <a href="v2.pdf">v2.pdf</a>  17-Aug-2019 06:24             1223356
      </pre>
    <hr>
  </body>
</html>

Upvotes: 2

Views: 1463

Answers (1)

Danila Vershinin
Danila Vershinin

Reputation: 9855

It should be pretty straightforward with autoindex_format:

location /docs {
    alias /home/john/docs/;
    autoindex on;
    autoindex_format json;
}

Upvotes: 4

Related Questions