Sabtu, 18 November 2023, 10:40 pm48
In PHP world, webserver with autoindex module (e.g Apache mod_autoindex, Litespeed Auto Index, nginx ngx_http_autoindex_module) already provide a way to display file listing in a public directory – we don’t even need PHP to have this feature.
In NodeJS however, since it is a more general purpose language, NodeJS itself functions as a webserver, with its
http module (built-in, low level API), or by using
express module (web app framework).
express middleware, named
serve-index, provides this functionality to display file listing of a public directory.
To demonstrate how this work, follow this tutorial:
mkdir static_server cd static_server npm init -y npm install -y express serve-index mkdir public touch public/test.txt echo "helo wolf" > public/test.txt touch index.js
Use this source code for
Now you can run the app:
Check the result in web browser,
When clicking on the file link, you can see the content in browser if supported. If it’s playable audio or video, then the browser will play it. Or else will prompt to download it, similar behavior of common static webservers.