Skip to main content
This guide will walk you through setting up the Tandex Electronics API on your local machine, from cloning the repository to making your first authenticated API request.

Prerequisites

Before you begin, ensure you have the following installed:
  • Node.js (v12 or higher)
  • npm (comes with Node.js)
  • MySQL (v5.7 or higher)
  • Git

Setup Instructions

1

Clone the repository

Clone the Tandex Electronics API repository to your local machine:
git clone <repository-url>
cd backend
2

Install dependencies

Install all required Node.js packages using npm:
npm install
This will install the following key dependencies:
  • express (4.17.1)
  • jsonwebtoken (8.5.1)
  • bcrypt (5.0.1)
  • mysql (2.18.1)
  • @woocommerce/woocommerce-rest-api (1.0.1)
  • And other supporting packages
3

Configure environment settings

Create your configuration file at config/default.json with your database credentials and JWT secret:
config/default.json
{
    "app": {
        "port": 8080,
        "JWT_SECRET": "YOUR_JWT_SECRET_HERE"
    },
    "db": {
        "host": "your-database-host",
        "db": "your-database-name",
        "user": "your-database-user",
        "password": "your-database-password"
    }
}
Replace the placeholder values with your actual database credentials. Keep your JWT_SECRET secure and never commit it to version control.
Configuration Parameters:
  • app.port - Port number for the server (default: 8080)
  • app.JWT_SECRET - Secret key for JWT token signing and verification
  • db.host - MySQL database host address
  • db.db - Database name
  • db.user - Database username
  • db.password - Database password
4

Set up MySQL database

Create the required MySQL database and tables. Connect to your MySQL server:
mysql -u your-username -p
Create the database:
CREATE DATABASE heroku_1a378f873641606;
USE heroku_1a378f873641606;
The API requires the following tables: usuarios, productossae, productos_publicados, and bitacora. Refer to your database schema documentation for the complete table structure.
Key Tables:
  • usuarios - User accounts with encrypted passwords
  • productossae - Product catalog with pricing and inventory
  • productos_publicados - Tracks products published to WooCommerce
  • bitacora - Audit log for user actions
5

Start the server

You can start the server in two modes:
npm start
Development mode uses nodemon for automatic server restarts on file changes.You should see the server start successfully. The API will be available at:
http://localhost:8080
The server is configured in server.js:31 to listen on the port specified in your config or environment variable PORT.
6

Test the API

Verify the API is running by making a request to the health check endpoint:
curl http://localhost:8080/
Expected response:
{
  "msg": "Api de TANDEX online"
}
This confirms your API is up and running successfully!
7

Authenticate and get your token

To access protected endpoints, you need to authenticate. First, ensure you have a user account in the database.Make a login request:
curl -X POST http://localhost:8080/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "your_password"
  }'
Successful authentication returns:
{
  "msg": "Autenticación correcta",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}
Save this token! You’ll need it to access all secured endpoints. The token expires after 1 hour.
8

Make an authenticated request

Use your JWT token to access a protected endpoint. For example, get the product list:
curl http://localhost:8080/secured/getProductos \
  -H "Authorization: YOUR_JWT_TOKEN_HERE"
Replace YOUR_JWT_TOKEN_HERE with the actual token from the login response.Successful response:
{
  "status": 200,
  "productos": [
    {
      "claveProducto": "PROD001",
      "descripcionP": "Electronic Component",
      "precio": "299.99",
      "existencias": 50
    }
  ]
}

Server Configuration Details

The server (server.js) is configured with the following middleware:
server.js
app.use(cors())                              // Enable CORS
app.use(morgan('dev'))                       // HTTP request logging
app.use(express.urlencoded({extended: true})) // Parse URL-encoded bodies
app.use(express.json())                      // Parse JSON bodies
app.use(express.json({limit: '100mb'}))      // Support large payloads
The server also implements cache control headers to prevent caching of API responses:
server.js:21-27
app.use((req,res,next)=>{
    res.header("Cache-Control", "no-cache, no-store, must-revalidate");
    res.header("Pragma", "no-cache");
    res.header("Expires", 0);
    next()
})

Common Issues

  • Verify your MySQL server is running
  • Check database credentials in config/default.json
  • Ensure the database exists and user has proper permissions
  • Test connection: mysql -h host -u user -p
  • Another process is using port 8080
  • Change the port in config/default.json or set the PORT environment variable:
    PORT=3000 npm start
    
  • Ensure JWT_SECRET is properly configured in config/default.json
  • The JWT secret is loaded from config on server startup (see server.js:9)
  • Verify your JWT token is valid and not expired (tokens expire after 1 hour)
  • Ensure you’re including the token in the Authorization header
  • Check that your token was received from a successful login

Next Steps

Now that your API is running, you can:
  • Learn more about Authentication and JWT token management
  • Explore the API endpoints in the API Reference section
  • Set up WooCommerce integration for product publishing
  • Create user accounts with different access levels

Authentication Guide

Deep dive into JWT authentication and security

API Reference

Complete API endpoint documentation

Build docs developers (and LLMs) love