Static web server autoindex listing using NodeJS
Sabtu, 18 November 2023, 10:40 pm0
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).
One of 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 index.js
:
Now you can run the app:
node index.js
Check the result in web browser, http://localhost:8080
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.
Melawat rumah kelahiran Tun M (17 gambar wide angle)
20 November 2023
Episod terakhir Jeon So Min bersama Running Man
15 November 2023