const express = require('express');
const mysql = require('mysql2');

const app = express();
const port = 3306;

// Create a database connection
const db = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'ezserverdb',
  database: 'ezserverdb',
});

// Connect to the database
db.connect((err) => {
  if (err) {
    console.error('Error connecting to database:', err);
    return;
  }
  console.log('Connected to the database');
});

// Define a route to retrieve and return user data
app.get('/users', (req, res) => {
  const query = 'SELECT * FROM users';
  db.query(query, (err, results) => {
    if (err) {
      console.error('Error querying the database:', err);
      res.status(500).json({ error: 'An error occurred' });
      return;
    }
    res.json(results); // Send user data as JSON response
  });
});

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