diff --git a/server.js b/server.js index 5adde52..d055bad 100644 --- a/server.js +++ b/server.js @@ -5,84 +5,92 @@ const fs = require('fs'); const app = express(); +// configure storage for uploaded files const storage = multer.diskStorage({ - destination: (req, file, cb) => { - cb(null, 'uploads/'); - }, - filename: (req, file, cb) => { - const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9); - cb(null, uniqueSuffix + '-' + file.originalname); - } + destination: (req, file, cb) => { + cb(null, 'uploads/'); + }, + filename: (req, file, cb) => { + const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9); + cb(null, uniqueSuffix + '-' + file.originalname); + } }); const upload = multer({ storage: storage }); +// serve static files app.use(express.static(__dirname)); +// upload files app.post('/upload', upload.array('files'), (req, res) => { - if (!req.files || req.files.length === 0) { - return res.status(400).send('No files were uploaded.'); - } - res.send(`Successfully uploaded ${req.files.length} file(s)!`); + if (!req.files || req.files.length === 0) { + return res.status(400).send('No files were uploaded.'); + } + res.send(`Successfully uploaded ${req.files.length} file(s)!`); }); +// get list of uploaded files app.get('/files', (req, res) => { - const directoryPath = path.join(__dirname, 'uploads'); - fs.readdir(directoryPath, (err, files) => { - if (err) { - return res.status(500).send('Unable to scan directory: ' + err); - } - const fileListPromises = files.map(file => { - return new Promise((resolve) => { - const filePath = path.join(directoryPath, file); - fs.stat(filePath, (err, stats) => { - if (err) { - return resolve(null); - } - resolve({ - name: file, - path: `/uploads/${file}`, - size: stats.size, - date: stats.mtime - }); - }); - }); - }); + const directoryPath = path.join(__dirname, 'uploads'); + fs.readdir(directoryPath, (err, files) => { + if (err) { + return res.status(500).send('Unable to scan directory: ' + err); + } + const fileListPromises = files.map(file => { + return new Promise((resolve) => { + const filePath = path.join(directoryPath, file); + fs.stat(filePath, (err, stats) => { + if (err) { + return resolve(null); + } + resolve({ + name: file, + path: `/uploads/${file}`, + size: stats.size, + date: stats.mtime + }); + }); + }); + }); - Promise.all(fileListPromises).then(fileList => { - res.json(fileList.filter(file => file !== null)); - }); - }); + Promise.all(fileListPromises).then(fileList => { + res.json(fileList.filter(file => file !== null)); + }); + }); }); +// serve a specific file app.get('/uploads/:filename', (req, res) => { - const filePath = path.join(__dirname, 'uploads', req.params.filename); - res.sendFile(filePath, err => { - if (err) { - res.status(404).send('File not found'); - } - }); + const filePath = path.join(__dirname, 'uploads', req.params.filename); + res.sendFile(filePath, err => { + if (err) { + res.status(404).send('File not found'); + } + }); }); +// download a specific file app.get('/download/:filename', (req, res) => { - const filePath = path.join(__dirname, 'uploads', req.params.filename); - res.download(filePath, err => { - if (err) { - res.status(404).send('File not found'); - } - }); + const filePath = path.join(__dirname, 'uploads', req.params.filename); + res.download(filePath, err => { + if (err) { + res.status(404).send('File not found'); + } + }); }); +// delete a specific file app.delete('/delete/:filename', (req, res) => { - const filePath = path.join(__dirname, 'uploads', req.params.filename); - fs.unlink(filePath, err => { - if (err) { - return res.status(404).send('File not found'); - } - res.send('File deleted successfully'); - }); + const filePath = path.join(__dirname, 'uploads', req.params.filename); + fs.unlink(filePath, err => { + if (err) { + return res.status(404).send('File not found'); + } + res.send('File deleted successfully'); + }); }); +// start the server app.listen(3000, () => { - console.log('Server is running on http://localhost:3000'); -}); + console.log('Server is running on http://localhost:3000'); +}); \ No newline at end of file