From b5c3c046f17eb28555f9398bcbc741ae8240676b Mon Sep 17 00:00:00 2001 From: HsiangNianian Date: Mon, 19 Aug 2024 17:58:55 +0800 Subject: chore: update content --- index.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 index.js (limited to 'index.js') diff --git a/index.js b/index.js new file mode 100644 index 0000000..51c542b --- /dev/null +++ b/index.js @@ -0,0 +1,30 @@ +const express = require('express'); +const fs = require('fs'); +const path = require('path'); + +const app = express(); +const PORT = 3000; + +app.use(express.static('public')); + +app.get('/api/files', (req, res) => { + const directoryPath = path.join(__dirname, 'files'); + const getDirectoryTree = (dirPath) => { + const files = fs.readdirSync(dirPath); + return files.map(file => { + const filePath = path.join(dirPath, file); + const isDirectory = fs.statSync(filePath).isDirectory(); + return { + name: file, + path: filePath, + isDirectory, + children: isDirectory ? getDirectoryTree(filePath) : [] + }; + }); + }; + res.json(getDirectoryTree(directoryPath)); +}); + +app.listen(PORT, () => { + console.log(`Server is running on http://localhost:${PORT}`); +}); \ No newline at end of file -- cgit v1.2.3-70-g09d2