frontend-backend-database

 How it works (step by step)

  1. Frontend sends a request
  2. GET /api/animals
  3. Node.js backend receives the request
  4. router.get('/', (req, res) => { ... });
  5. Node.js executes an SQL query
  6. SELECT * FROM animals;
  7. Database returns data
  8. Node.js sends JSON back to frontend

Example: Node.js SQL Query (READ)

router.get('/animals', (req, res) => {

  const sql = 'SELECT * FROM animals';

 

  db.query(sql, (err, results) => {

    if (err) {

      return res.status(500).json({ error: err.message });

    }

    res.json(results);

  });

});

What’s happening here?

Part

Meaning

router.get()

REST API endpoint

SELECT * FROM animals

SQL query

db.query()

Node.js sends SQL to MySQL

res.json(results)

Data returned to frontend

👉 This is a Node.js SQL query.

Comments

Popular posts from this blog

Python_While_Loop

Python_Lists_Loops

clinical_app