Skip to content

Instantly share code, notes, and snippets.

@jamesr2323
Created July 18, 2023 23:01
Show Gist options
  • Save jamesr2323/ba8671bb15eac88e157e5bb888bebf70 to your computer and use it in GitHub Desktop.
Save jamesr2323/ba8671bb15eac88e157e5bb888bebf70 to your computer and use it in GitHub Desktop.
const express = require('express');
const fs = require('fs-extra');
const path = require('path');
const app = express();
const port = 3000;
app.use(express.static('./'));
app.get('/', async (req, res) => {
try {
const directoryPath = '.'; // Set your directory path here
const files = await fs.readdir(directoryPath);
const fileInfo = await Promise.all(files.map(async file => {
const filePath = path.join(directoryPath, file);
const stats = await fs.lstat(filePath);
return {
name: file,
path: filePath,
isDirectory: stats.isDirectory(),
size: stats.size,
};
}));
res.json(fileInfo);
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment