Skip to main content

Overview

The Tandex Electronics API is an Express.js-based REST API service that provides product management, user authentication, and WooCommerce integration capabilities.

System Requirements

Before you begin, ensure you have the following installed:
  • Node.js: Version 12.x or higher (compatible with the dependencies)
  • MySQL: Version 5.7 or higher
  • npm: Version 6.x or higher

Installation

1

Clone the repository

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

Install dependencies

Install all required npm packages:
npm install
This will install the following key dependencies:
  • express - Web framework
  • mysql - Database driver
  • jsonwebtoken - JWT authentication
  • bcrypt - Password hashing
  • @woocommerce/woocommerce-rest-api - WooCommerce integration
  • cors - Cross-origin resource sharing
  • morgan - HTTP request logger
3

Configure environment variables

Create a config/default.json file in the project root with your configuration:
{
  "app": {
    "port": 8080,
    "JWT_SECRET": "your-secure-jwt-secret-key"
  },
  "db": {
    "host": "your-database-host",
    "db": "your-database-name",
    "user": "your-database-user",
    "password": "your-database-password"
  }
}
Keep your JWT_SECRET secure and never commit it to version control. Use a strong, randomly generated string.
4

Set up the database

Import the database schema from backend.sql:
mysql -u your-username -p your-database-name < backend.sql
See the Database Configuration guide for detailed setup instructions.
5

Start the server

Run the server in development or production mode:
npm run dev
Development mode uses nodemon for automatic server restart on file changes.

Server Configuration

The server is configured in server.js with the following settings:
server.js
const express = require('express')
const server_config = require('config')
const http = require('http')
const cors = require('cors')
const morgan = require('morgan')
const puerto = process.env.PORT || 8080

process.env.JWT_SECRET = (server_config.get('app.JWT_SECRET'))

var app = express()

app.use(cors())
app.use(morgan('dev'));
app.use(express.urlencoded({extended : true})) 
app.use(express.json());
app.use(express.json({limit : '100mb'}))

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()
})

app = require('./routes/setup/routes_setup').setup(app,express)

http.createServer(app).listen(puerto)

Key Features

  • CORS enabled: Allows cross-origin requests from any domain
  • Request logging: Morgan middleware logs all HTTP requests in development mode
  • JSON payload: Supports up to 100MB JSON payloads
  • Cache control: Prevents browser caching with appropriate headers
  • Port configuration: Uses PORT environment variable or defaults to 8080

Verifying the Installation

Once the server is running, verify the installation:
  1. Check the console output for any errors
  2. The server should be listening on the configured port (default: 8080)
  3. Test the API endpoint:
curl http://localhost:8080/api/health
If you encounter any connection errors, verify that:
  • MySQL is running and accessible
  • Database credentials in config/default.json are correct
  • The database schema has been imported successfully

Port Configuration

The server port can be configured in two ways:
  1. Environment variable: Set the PORT environment variable
    PORT=3000 npm start
    
  2. Config file: Modify the port value in config/default.json
    {
      "app": {
        "port": 3000
      }
    }
    
The environment variable PORT takes precedence over the config file setting.

Next Steps

Build docs developers (and LLMs) love