Skip to main content

Introduction

Mis Compras uses a dual-backend architecture that combines Node.js and PHP to provide a robust and flexible e-commerce API. This hybrid approach leverages the strengths of both technologies to deliver optimal performance and maintainability.
The platform runs two separate backend servers: a Node.js server on port 4000 for modern REST API operations, and PHP scripts for legacy authentication and user management.

Architecture Overview

Node.js Backend (Express)

The primary API server runs on Express.js and handles core e-commerce operations:
  • Base URL: http://localhost:4000
  • Technology Stack: Express.js, MySQL/MySQL2
  • Features: Product management, order processing, user registration

PHP Backend

Legacy authentication and user management system:
  • Location: /php/ directory
  • Technology Stack: PHP 7+, MySQL
  • Features: Session-based authentication, user login/registration
Both backends connect to the same MySQL database. Ensure database consistency when making schema changes.

API Endpoints Structure

Node.js Endpoints

The Express server exposes three main route groups:
// Base: /api/productos
GET    /api/productos           // Get all products

PHP Endpoints

// Registration
POST   /php/registro.php

// Login
POST   /php/login.php

Response Format

All API endpoints return JSON responses with a consistent structure:

Success Response

{
  "success": true,
  "message": "Operation completed successfully",
  "data": { /* relevant data */ }
}

Error Response

{
  "success": false,
  "message": "Error description",
  "error": "Detailed error message"
}
PHP endpoints may use Spanish field names (e.g., éxito instead of success). Check the authentication documentation for details.

CORS Configuration

Both backends are configured with CORS enabled:
  • Node.js: Uses cors middleware with default settings
  • PHP: Headers set to allow all origins (Access-Control-Allow-Origin: *)
Node.js CORS
app.use(cors());
PHP CORS
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");

Request Format

Node.js Endpoints

All Node.js endpoints expect JSON payloads:
curl -X POST http://localhost:4000/api/pedidos/checkout \
  -H "Content-Type: application/json" \
  -d '{"usuario_id": 1, "items": [...], "total": 150.00}'

PHP Endpoints

PHP endpoints expect form-data (application/x-www-form-urlencoded):
curl -X POST http://localhost/php/login.php \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "[email protected]&contrasena=password123"
Note the difference: Node.js uses JSON, PHP uses form-data. This is critical for proper request handling.

Error Handling

Node.js Error Handling

try {
  const [rows] = await db.query("SELECT * FROM productos");
  res.json(rows);
} catch (err) {
  console.error(err);
  res.status(500).json({ error: "Error al obtener productos" });
}

PHP Error Handling

if (empty($email) || empty($contrasena)) {
    echo json_encode(["éxito" => false, "mensaje" => "Faltan campos."]);
    exit;
}

Database Connection

Both backends connect to the same MySQL database:
  • Node.js: Uses mysql2 with promise-based connections
  • PHP: Uses mysqli with prepared statements
All database queries use prepared statements to prevent SQL injection attacks.

Server Status

Check if the Node.js server is running:
curl http://localhost:4000/
Expected Response:
Servidor de Tienda Online funcionando ✅

Next Steps

Authentication

Learn about session management and authentication flows

Products API

Explore product catalog endpoints

Orders API

Process orders and manage checkout

Users API

Manage user accounts and profiles

Build docs developers (and LLMs) love