nodejs api find data with id

const express = require('express');
const app = express();
const PORT = 3000;

// Assuming you have data stored in an array
const data = [
  { id: 1, name: 'Item 1' },
  { id: 2, name: 'Item 2' },
  { id: 3, name: 'Item 3' }
];

// Endpoint to find data by ID
app.get('/api/data/:id', (req, res) => {
  const id = parseInt(req.params.id);

  const item = data.find(item => item.id === id);

  if (item) {
    res.json(item);
  } else {
    res.status(404).json({ error: 'Item not found' });
  }
});

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