refresh after delete in node

// Import required modules
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');

// Create an Express application
const app = express();
const port = 3000;

// Use body-parser middleware to parse JSON requests
app.use(bodyParser.json());

// Define a route for handling DELETE requests
app.delete('/api/items/:id', (req, res) => {
  // Get the ID parameter from the request
  const itemId = req.params.id;

  // Read the existing data from the file
  const rawData = fs.readFileSync('data.json');
  const items = JSON.parse(rawData);

  // Find and remove the item with the specified ID
  const updatedItems = items.filter(item => item.id !== itemId);

  // Write the updated data back to the file
  fs.writeFileSync('data.json', JSON.stringify(updatedItems));

  // Send a success response
  res.status(200).json({ message: 'Item deleted successfully' });
});

// Define a route for handling GET requests to fetch all items
app.get('/api/items', (req, res) => {
  // Read the data from the file
  const rawData = fs.readFileSync('data.json');
  const items = JSON.parse(rawData);

  // Send the list of items as a response
  res.status(200).json(items);
});

// Start the Express server
app.listen(port, () => {
  console.log(`Server is listening on port ${port}`);
});

This Node.js code defines an Express application with two routes. The first route handles DELETE requests to delete an item with a specific ID from a JSON file. The second route handles GET requests to fetch all items from the same JSON file. The application uses the body-parser middleware to parse JSON requests. The fs module is used to read and write data to a JSON file named 'data.json'.