Prerequisites
Before you begin, ensure you have the following installed:Node.js
Version 18 or higher
PostgreSQL
Version 12 or higher
Git
For cloning the repository
Installation
Follow these steps to get the DAF Backend API running on your local machine:Install dependencies
Install all required npm packages:This will install Express.js, PostgreSQL client (pg), JWT, Multer, and all other dependencies listed in
package.json.Configure environment variables
Create a
.env file in the root directory with the following variables:Set up PostgreSQL databases
Create two PostgreSQL databases:
For the POS database, you’ll create individual user accounts with specific permissions. These users will authenticate through the API.
Run database migrations
Run your database schema migrations to create all required tables. The API expects tables for:POS Database:
cliente, producto, factura, proveedor, materiaprima, categoria, kardex_mp, kardex_prod, etc.ECOM Database: usuario, cliente, producto, carrito, detalle_carrito, ciudad, etc.Verify installation
Test that the API is running correctly:- Health Check
- Image Directory
Try accessing a non-existent route to verify the 404 handler:Expected response:
Make your first API call
Now let’s authenticate and make an API call:Authenticate with POS system
First, create a PostgreSQL user for POS access:Then authenticate via the API:
Use the JWT token
Save the token from the response and use it in subsequent requests:This will return a paginated list of clients from the POS database.
CORS configuration
The API is configured to accept requests from specific origins:src/app.js:29-35
By default, the API accepts requests from
http://localhost:5173 and http://localhost:5174. Modify FRONTEND_IP in your .env file to add your frontend’s URL.Common issues
Database connection errors
Database connection errors
Error:
Credenciales incorrectas o error de conexiónSolutions:- Verify PostgreSQL is running:
pg_isready - Check database names in
.envmatch actual databases - Ensure database users have correct permissions
- For POS: Verify the user exists in PostgreSQL
- For ECOM: Check
EC_USERandEC_PASSWORDin.env
Token invalid errors
Token invalid errors
Error:
Token inválido o expiradoSolutions:- Check that
JWT_SECRETin.envhasn’t changed - Verify token is being sent in
Authorization: Bearer <token>format - Token may have expired (default 24h) - login again
Port already in use
Port already in use
Error:
EADDRINUSE: address already in use ::3000Solutions:- Change
PORTin.envto a different value - Kill the process using port 3000:
lsof -ti:3000 | xargs kill
Image upload fails
Image upload fails
Error:
Solo se permiten imágenesSolutions:- Ensure you’re sending image files (JPEG, PNG, etc.)
- Check file size is under 5MB limit
- Verify
src/imagesdirectory exists and is writable
Next steps
Authentication
Learn about JWT structure and credential-based connections
Architecture
Understand the dual-database architecture
POS API Endpoints
Explore all available POS endpoints
Error Handling
Learn about error responses and debugging