const express = require("express"); const fs = require("fs"); const path = require("path"); const app = express(); //讀取當前目錄中 public檔案中所有檔案 const directorPath = path.join(__dirname,"public"); app.get("/",(req,res)=>{ fs.readdir(directorPath,(err,files)=>{ if(err){ return res.status(500),send("無法讀取目錄") } let content = ""; //public 檔案下所有目錄列表 files.forEach(item=>{ content+=`<h2 onclick='window.location.href="/public/${item}"'>${item}</h2>` }) res.send(content); }) }) app.use("/public",express.static("./public")); app.listen(80,()=>{ console.log("express server running at http://192.168.2.15") })