Sobes.tech
Junior — Middle

How is data exchanged between the user interface and server-side services via API?

sobes.tech AI

Answer from AI

Data exchange between the user interface (UI) and server-side services typically occurs over the HTTP(S) protocol using data formats such as JSON or XML.

  1. The UI sends an HTTP request (GET, POST, PUT, DELETE) to the API server.
  2. The server processes the request, performs necessary operations, and returns a response with data.
  3. The UI receives the response, parses the data, and updates the display.

In Node.js, the Express framework is often used to implement APIs:

const express = require('express');
const app = express();
app.use(express.json());

// Example API endpoint
app.post('/api/data', (req, res) => {
  const receivedData = req.body;
  // Process data
  res.json({ message: 'Data received', data: receivedData });
});

app.listen(3000, () => console.log('Server running on port 3000'));

On the client side (for example, in a browser), fetch can be used to send requests:

fetch('/api/data', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ key: 'value' })
})
.then(response => response.json())
.then(data => console.log(data));