Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/jperez77775/ProyectoDocker/llms.txt

Use this file to discover all available pages before exploring further.

The backend service is a lightweight Node.js/Express API that reads CV data from MySQL and exposes it over HTTP. It runs on port 4000 inside its container and handles the full lifecycle of the database connection, including automatic retry on startup and reconnection if the link drops while the service is running.

Technology stack

ComponentVersion
Node.js20 (Alpine)
Express4.18.x
mysql23.6.x
cors2.8.x
The service listens on port 4000, which is mapped directly to port 4000 on the host in docker-compose.yml.

API endpoint

The backend exposes a single endpoint:
MethodPathDescription
GET/cvReturns the full CV object including persona data and education list
For full request/response details, see the API Reference section.

Connection retry logic

Because Docker containers start in parallel and MySQL takes several seconds to become ready, the backend uses a connectWithRetry() function that keeps attempting the connection until it succeeds. If the initial connection fails, it waits 5 seconds and tries again. Once connected, it also listens for runtime errors and reconnects automatically when it detects a dropped link.
server.js
const connectWithRetry = () => {
  if (isConnecting) return;
  isConnecting = true;

  console.log('Intentando conectar a la base de datos MySQL...');

  if (db) {
    try { db.destroy(); } catch(e) {}
  }

  db = mysql.createConnection({
    host: 'database',
    user: 'root',
    password: 'rootpassword',
    database: 'cv_db',
    charset: 'utf8mb4'
  });

  db.connect((err) => {
    isConnecting = false;

    if (err) {
      console.error('Error al conectar a MySQL, reintentando en 5 segundos...', err.message);
      setTimeout(connectWithRetry, 5000);
    } else {
      console.log('¡Conectado exitosamente a la base de datos MySQL!');
      db.query("SET NAMES utf8mb4;");
    }
  });

  db.on('error', (err) => {
    isConnecting = false;
    console.error('Error crítico detectado en el cliente MySQL:', err.message);
    if (err.code === 'PROTOCOL_CONNECTION_LOST' || err.code === 'ECONNREFUSED') {
      connectWithRetry();
    }
  });
};
The isConnecting guard prevents duplicate connection attempts from stacking up. The error handler covers two specific codes:
  • PROTOCOL_CONNECTION_LOST — the TCP connection was closed unexpectedly
  • ECONNREFUSED — the database container is not yet accepting connections
The backend service declares depends_on: database: condition: service_healthy in docker-compose.yml. This means Docker waits for MySQL’s health check to pass before starting the backend container, reducing the number of retries needed in practice.

UTF-8 encoding fix

MySQL can return strings as latin1-encoded bytes when the connection charset is misconfigured. The sanearTexto() function works around this by re-reading each string through a binary buffer and decoding it as UTF-8, which correctly reconstructs accented characters like é and ó that would otherwise appear corrupted.
server.js
const sanearTexto = (texto) => {
  if (!texto) return texto;
  const buffer = Buffer.from(texto, 'binary');
  return buffer.toString('utf8');
};
This function is applied to every text field in the response — nombre, apellido, ciudad, titulo, and institucion — before the JSON is sent to the client.

Dockerfile

The backend image is built from a minimal Alpine-based Node 20 image. Dependencies are installed before copying the full source so that Docker can cache the npm install layer and skip it on rebuilds that only change application code.
Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD ["node", "server.js"]

Environment configuration

The database connection is configured with hardcoded values in server.js:
ParameterValue
hostdatabase
userroot
passwordrootpassword
databasecv_db
charsetutf8mb4
The hostname database resolves to the database container via the shared cv_network Docker bridge network defined in docker-compose.yml.
To inspect the backend’s output and debug connection issues, run docker compose logs backend. Add the -f flag to stream logs in real time.

Build docs developers (and LLMs) love