{"library":"lws-index","title":"Lws Directory Listing Middleware","description":"lws-index is a middleware plugin designed for the Lightweight Web Server (lws) ecosystem, enabling the serving of directory listings. It functions as a wrapper around the popular `serve-index` library, integrating its capabilities seamlessly into lws. The current stable version is 3.1.1, and being part of the lws family, it generally follows the lws release cadence, focusing on stability and compatibility within that ecosystem. Its key differentiator is its straightforward integration as a plugin for lws, providing configurable options like specifying a root directory, showing hidden files, and choosing between 'tiles' or 'details' view modes, without needing manual Express.js-style middleware setup. It is often used in conjunction with `lws-static` for comprehensive file serving.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install lws-index"],"cli":null},"imports":["import lwsIndex from 'lws-index';","import Lws from 'lws';","import lwsIndex, { Options } from 'lws-index';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Lws from 'lws';\nimport lwsIndex from 'lws-index';\nimport lwsStatic from 'lws-static';\nimport { createRequire } from 'module';\nimport path from 'path';\nimport { fileURLToPath } from 'url';\n\nconst __dirname = path.dirname(fileURLToPath(import.meta.url));\nconst require = createRequire(import.meta.url);\n\n// Ensure a directory exists for testing\nimport { mkdirSync, writeFileSync } from 'fs';\nconst publicDir = path.join(__dirname, 'public');\nmkdirSync(publicDir, { recursive: true });\nwriteFileSync(path.join(publicDir, 'index.html'), '<h1>Hello from Lws!</h1>');\nmkdirSync(path.join(publicDir, 'subfolder'), { recursive: true });\nwriteFileSync(path.join(publicDir, 'subfolder', 'test.txt'), 'This is a test file.');\nwriteFileSync(path.join(publicDir, '.hiddenfile'), 'You should not see this by default.');\n\nconst PORT = process.env.PORT || 8000;\n\nnew Lws().listen({\n  port: PORT,\n  directory: publicDir, // This sets the default root for lws-static and lws-index\n  stack: [\n    lwsStatic(), // Serve static files first\n    lwsIndex({\n      root: publicDir, // Explicitly set the root for the index listing\n      hidden: false,  // Do not show hidden files by default\n      view: 'tiles'   // Use the 'tiles' display mode\n    })\n  ]\n}).on('started', ({ url }) => {\n  console.log(`Lws server with directory listing started on ${url}`);\n  console.log(`Try navigating to ${url}/subfolder/`);\n});","lang":"typescript","description":"This example demonstrates how to set up `lws` programmatically, configuring it with both `lws-static` to serve files and `lws-index` to provide directory listings. It creates a sample directory structure and starts a server that will show directory contents when navigating to a folder.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}