using multiparty with node js express

// Step 1: Install the 'multiparty' module
npm install multiparty

// Step 2: Require the necessary modules in your Node.js Express application
const express = require('express');
const multiparty = require('multiparty');
const fs = require('fs');

// Step 3: Create an Express app instance
const app = express();

// Step 4: Define a route that handles file uploads
app.post('/upload', (req, res) => {
  // Step 5: Create a new multiparty form
  const form = new multiparty.Form();

  // Step 6: Parse the incoming request
  form.parse(req, (err, fields, files) => {
    if (err) {
      // Handle error
      res.status(500).send('Error uploading file');
      return;
    }

    // Step 7: Access the uploaded file details
    const file = files.file[0];

    // Step 8: Specify the file destination path
    const newPath = __dirname + '/uploads/' + file.originalFilename;

    // Step 9: Read the file stream and write it to the destination
    fs.readFile(file.path, (err, data) => {
      if (err) {
        // Handle error
        res.status(500).send('Error reading file');
        return;
      }

      // Step 10: Write the file to the specified path
      fs.writeFile(newPath, data, (err) => {
        if (err) {
          // Handle error
          res.status(500).send('Error writing file');
          return;
        }

        // Step 11: Send a success response
        res.send('File uploaded successfully');
      });
    });
  });
});

// Step 12: Start the Express server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});