npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

autoindex-json

v1.1.1

Published

Serves directory contents and file info for a given path in JSON format

Downloads

3

Readme

npm npm npm

Serves directory contents and file info for a given path in JSON format. Output adheres to the NGINX json autoindex format. Supports very large folders with pagination and exists queries.

Installation

Install it using npm or yarn

npm install autoindex-json
yarn add autoindex-json

Usage

Use it in your node server app

const express = require('express');
const autoindexJson = require('autoindex-json');

const app = express();
const DATA_DIR = '/path/to/your/data';
app.use('/files', autoindexJson(DATA_DIR), express.static(DATA_DIR));
app.listen(3000);

Then to get the info JSON response, simply append ?info to the required path in the request URL. For directories, autoindex_json will be served by default, hence, ?info is optional. Here are some example requests and JSON responses:

GET http://localhost:3000/files                     # or
GET http://localhost:3000/files?info
[{"name":"bdd100k","type":"directory","mtime":"Tue, 02 Feb 2021 10:26:42 GMT"},
{"name":"cmu_seasons","type":"directory","mtime":"Fri, 17 Jul 2020 15:54:36 GMT"},
{"name":"cyrill_bonn","type":"directory","mtime":"Thu, 18 Apr 2019 10:18:44 GMT"},
{"name":"kitti","type":"directory","mtime":"Mon, 15 Feb 2021 11:54:58 GMT"},
{"name":"new_college","type":"directory","mtime":"Thu, 28 Jan 2021 02:11:52 GMT"},
{"name":"newer_college","type":"directory","mtime":"Sun, 31 Jan 2021 15:48:01 GMT"},
{"name":"oxford_robotcar","type":"directory","mtime":"Mon, 15 Jun 2020 13:22:01 GMT"}]
GET http://localhost:3000/files/new_college         # or
GET http://localhost:3000/files/new_college?info
[{"name":"README.md","type":"file","mtime":"Thu, 28 Jan 2021 02:07:03 GMT","size":2280},
{"name":"city_centre","type":"directory","mtime":"Thu, 28 Jan 2021 02:15:21 GMT"},
{"name":"new_college","type":"directory","mtime":"Thu, 28 Jan 2021 02:12:51 GMT"}]
GET http://localhost:3000/files/new_college/README.md?info
{"name":"README.md","type":"file","mtime":"Thu, 28 Jan 2021 02:07:03 GMT","size":2280}
GET http://localhost:3000/files/new_college/README.md
This is a *sample* README file content handled by express.static

This output conforms to NGINX JSON autoindex output standards

location / {
    autoindex on;
    autoindex_format json;
}

Note

We suggest using autoindexJson before express.static. Otherwise, someFile?info will result in express.static serving the file before autoindexJson can handle it.

If you would only like to serve info JSON for directories, you shall swap the order and it would still work.

Error

If the given path is not found or accessible, then the response will be 4xx with content

{"error":"File/Directory not found"}
{"error":"<Error Message>"}

The status code 4xx will get unavoidably logged in the browser console. So, if you want to avoid that, you can suppress settings setting the status alone and still get the same error JSON by setting onErrorStatus4xx option to false.

Exists

One can directly query the directory and

  • if the directory exists, the files and directories inside are listed
  • if not, a JSON with error field is returned.

However, this is not suitable for making a large number of exists queries as there is an unnecessary additional overhead of querying the directory contents.

By adding exists parameter, response is sent based only on whether the query path exists:

GET http://localhost:3000/files/oxford_robotcar?exists      # {"exists": true}
GET http://localhost:3000/files/oxford_1_robotcar?exists    # {"exists": false}

Pagination

Autoindex_json supports query based dynamic pagination. Use the limit parameter to retrieve information regarding the number of files and number of pages corresponding to the limit.

GET http://localhost:3000/files?limit=4
{
    "files": 6,
    "pages": 2
}

Then simply query the required page using page parameter.

GET http://localhost:3000/files?limit=4&page=0
[{"name":"bdd100k","type":"directory","mtime":"Tue, 02 Feb 2021 10:26:42 GMT"},
{"name":"cmu_seasons","type":"directory","mtime":"Fri, 17 Jul 2020 15:54:36 GMT"},
{"name":"cyrill_bonn","type":"directory","mtime":"Thu, 18 Apr 2019 10:18:44 GMT"},
{"name":"kitti","type":"directory","mtime":"Mon, 15 Feb 2021 11:54:58 GMT"},
{"name":"new_college","type":"directory","mtime":"Thu, 28 Jan 2021 02:11:52 GMT"},
]
GET http://localhost:3000/files?limit=4&page=1
[{"name":"newer_college","type":"directory","mtime":"Sun, 31 Jan 2021 15:48:01 GMT"},
{"name":"oxford_robotcar","type":"directory","mtime":"Mon, 15 Jun 2020 13:22:01 GMT"}]

Note that the pages queried should be indexed from 0 and not 1! If the page limit is exceeded, an empty array is returned.

GET http://localhost:3000/files?limit=4&page=2
[]

Note that the info parameter is implicit in the above examples. The following lines will yield the same response, as the statically served root is a directory.

GET http://localhost:3000/files?limit=4
GET http://localhost:3000/files?info&limit=4

Options

dir

string | Required

Path to directory to be served

options

json | optional

{
    onErrorStatus4xx: true,  // bool
}

If onErrorStatus4xx is true, status code is set to 4xx on error. Else, error messages will be delivered in JSON, but with a 2xx response status code.

Licence

Please see attached LICENSE