File Management System involves creating a Node.js and Express

File Management System involves creating a Node.js and Express

ยท

3 min read

Building a File Management System involves creating a Node.js and Express server that handles file upload, download, and deletion. Below is a step-by-step guide to create a simple File Management System.

Step 1: Set Up Your Project

  1. Create a new project folder:

     mkdir file-management-system
     cd file-management-system
    
  2. Initialize a new Node.js project:

     npm init -y
    
  3. Install necessary dependencies:

     npm install express multer fs path
    

Step 2: Create Your Folder Structure

Create the following folder structure:

file-management-system/
|-- uploads/
|-- src/
|   |-- routes/
|       |-- files.js
|   |-- app.js
|-- server.js

Step 3: Set Up Express Server

In app.js, set up an Express server:

const express = require('express');
const path = require('path');
const filesRoutes = require('./routes/files');

const app = express();

app.use(express.static(path.join(__dirname, 'uploads')));
app.use('/files', filesRoutes);

module.exports = app;

Step 4: Create Routes for File Management

In routes/files.js, set up routes for file management:

const express = require('express');
const router = express.Router();
const multer = require('multer');
const fs = require('fs');
const path = require('path');

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'uploads/');
  },
  filename: (req, file, cb) => {
    cb(null, Date.now() + path.extname(file.originalname));
  },
});

const upload = multer({ storage });

router.post('/upload', upload.single('file'), (req, res) => {
  res.json({ message: 'File uploaded successfully' });
});

router.get('/download/:filename', (req, res) => {
  const { filename } = req.params;
  const filePath = path.join(__dirname, '../uploads', filename);

  if (fs.existsSync(filePath)) {
    res.download(filePath);
  } else {
    res.status(404).json({ error: 'File not found' });
  }
});

router.delete('/delete/:filename', (req, res) => {
  const { filename } = req.params;
  const filePath = path.join(__dirname, '../uploads', filename);

  if (fs.existsSync(filePath)) {
    fs.unlinkSync(filePath);
    res.json({ message: 'File deleted successfully' });
  } else {
    res.status(404).json({ error: 'File not found' });
  }
});

module.exports = router;

Step 5: Run Your Application

In server.js, use the exported app to start the application:

const app = require('./src/app');

const PORT = process.env.PORT || 3000;

app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

Start the server:

node server.js

Step 6: Test File Upload, Download, and Deletion

  1. File Upload:

    Use a tool like Postman or Insomnia to send a POST request to http://localhost:3000/files/upload with a file attached. Ensure the file is sent with the key named file. You should receive a response indicating successful file upload.

  2. File Download:

    Open your browser and navigate to http://localhost:3000/files/download/{filename}, replacing {filename} with the actual filename of the uploaded file. This should prompt a download of the file.

  3. File Deletion:

    Use a tool like Postman or Insomnia to send a DELETE request to http://localhost:3000/files/delete/{filename}, replacing {filename} with the actual filename of the uploaded file. You should receive a response indicating successful file deletion.

Congratulations! You've created a simple File Management System with Node.js and Express. This example uses the multer middleware for file upload, and you can customize and extend it based on your specific requirements.

Did you find this article valuable?

Support Revive Coding by becoming a sponsor. Any amount is appreciated!

ย