If you don't have special design requirements, then the best way to handle directory listing in Node.js is nginx.
You can create the following directory structure with a special directory for listing:
- /home/username
- /app
- /www
- /listing
Now we have to instruct nginx to redirect all the requests to our app by using a proxy directive. Note that our special directory is not under the control of Node.js because we didn't specify a route to handle that particular path. It's up to nginx to handle it.
server {
server_name my.app;
listen 80;
root /home/username/www;
index index.html index.htm;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /listing {
autoindex on;
}
}