Configuration Guide
The Sales Management System API uses a centralized configuration file for all environment-specific settings.
Configuration File
The main configuration is located at src/config.js:
export default {
PORT: 3000 ,
HOST_DATABASE: "localhost" ,
PORT_DATABASE: 3306 ,
DATABASE_NAME: "erpDB" ,
PASSWORD_DATABASE: "admin" ,
USER_DATABASE: "root"
}
Configuration Parameters
Server Configuration
PORT
The port on which the Express server listens.
Type : number
Default : 3000
Example : PORT: 8080
Usage in code:
app . listen ( config . PORT , ( err ) => {
if ( err ) console . log ( err )
else {
console . log ( `Server on port http://localhost: ${ config . PORT } ` )
}
})
If port 3000 is already in use, change this to an available port like 3001 or 8080.
Database Configuration
HOST_DATABASE
MySQL server hostname or IP address.
Type : string
Default : "localhost"
Examples :
Local: "localhost" or "127.0.0.1"
Remote: "db.example.com" or "192.168.1.100"
PORT_DATABASE
MySQL server port.
Type : number
Default : 3306
Example : PORT_DATABASE: 3307 (for non-standard MySQL port)
DATABASE_NAME
Name of the MySQL database.
Type : string
Default : "erpDB"
Example : DATABASE_NAME: "sales_system"
Ensure this database exists before starting the application. Create it with:
USER_DATABASE
MySQL username for authentication.
Type : string
Default : "root"
Example : USER_DATABASE: "sales_api_user"
Best practice: Create a dedicated user with limited privileges:
CREATE USER ' sales_api_user '@ 'localhost' IDENTIFIED BY 'secure_password' ;
GRANT SELECT , INSERT , UPDATE , DELETE ON erpDB. * TO 'sales_api_user' @ 'localhost' ;
FLUSH PRIVILEGES;
PASSWORD_DATABASE
MySQL password for authentication.
Type : string
Default : "admin"
Example : PASSWORD_DATABASE: "my_secure_password_123"
NEVER commit passwords to version control! Use environment variables in production (see below).
Environment Variables (Recommended)
For production and team environments, use environment variables instead of hardcoded values.
Setup
Install dotenv (optional):
Create .env file:
# Server Configuration
PORT = 3000
# Database Configuration
DB_HOST = localhost
DB_PORT = 3306
DB_NAME = erpDB
DB_USER = root
DB_PASSWORD = admin
Update config.js:
import dotenv from 'dotenv' ;
dotenv . config ();
export default {
PORT: parseInt ( process . env . PORT ) || 3000 ,
HOST_DATABASE: process . env . DB_HOST || "localhost" ,
PORT_DATABASE: parseInt ( process . env . DB_PORT ) || 3306 ,
DATABASE_NAME: process . env . DB_NAME || "erpDB" ,
PASSWORD_DATABASE: process . env . DB_PASSWORD || "admin" ,
USER_DATABASE: process . env . DB_USER || "root"
}
Add .env to .gitignore:
echo ".env" >> .gitignore
System Environment Variables
Alternatively, set environment variables at the system level:
Linux/macOS:
export PORT = 3000
export DB_HOST = localhost
export DB_PORT = 3306
export DB_NAME = erpDB
export DB_USER = root
export DB_PASSWORD = admin
npm run dev
Windows (CMD):
set PORT = 3000
set DB_HOST = localhost
set DB_PORT = 3306
set DB_NAME = erpDB
set DB_USER = root
set DB_PASSWORD = admin
npm run dev
Windows (PowerShell):
$ env: PORT = 3000
$ env: DB_HOST = "localhost"
$ env: DB_PORT = 3306
$ env: DB_NAME = "erpDB"
$ env: DB_USER = "root"
$ env: DB_PASSWORD = "admin"
npm run dev
Connection Pool Configuration
The database connection pool is configured in src/model/pool.js:
import { createPool } from 'mysql2/promise' ;
import config from '../config.js' ;
export const pool = createPool ({
host: config . HOST_DATABASE ,
port: config . PORT_DATABASE ,
database: config . DATABASE_NAME ,
user: config . USER_DATABASE ,
password: config . PASSWORD_DATABASE
});
Advanced Pool Options
You can extend the pool configuration with additional options:
export const pool = createPool ({
host: config . HOST_DATABASE ,
port: config . PORT_DATABASE ,
database: config . DATABASE_NAME ,
user: config . USER_DATABASE ,
password: config . PASSWORD_DATABASE ,
// Connection pool settings
connectionLimit: 10 , // Max connections in pool
queueLimit: 0 , // No limit on queued requests
waitForConnections: true , // Wait for available connection
// Connection timeouts
connectTimeout: 10000 , // 10 seconds
acquireTimeout: 10000 , // 10 seconds
// Timezone
timezone: 'Z' , // UTC
// Character set
charset: 'utf8mb4' // Full Unicode support
});
Pool Configuration Options
Option Type Default Description connectionLimitnumber 10 Maximum connections in pool queueLimitnumber 0 Max queued connection requests (0 = unlimited) waitForConnectionsboolean true Wait for connection when pool is full connectTimeoutnumber 10000 Milliseconds before connection timeout acquireTimeoutnumber 10000 Milliseconds before acquire timeout timezonestring ’local’ Timezone for MySQL TIMESTAMP columns charsetstring ’UTF8_GENERAL_CI’ Character set for connection
Environment-Specific Configurations
Development
export default {
PORT: 3000 ,
HOST_DATABASE: "localhost" ,
PORT_DATABASE: 3306 ,
DATABASE_NAME: "erpDB_dev" ,
PASSWORD_DATABASE: "dev_password" ,
USER_DATABASE: "dev_user"
}
Testing
export default {
PORT: 3001 ,
HOST_DATABASE: "localhost" ,
PORT_DATABASE: 3306 ,
DATABASE_NAME: "erpDB_test" ,
PASSWORD_DATABASE: "test_password" ,
USER_DATABASE: "test_user"
}
Production
export default {
PORT: process . env . PORT || 80 ,
HOST_DATABASE: process . env . DB_HOST ,
PORT_DATABASE: parseInt ( process . env . DB_PORT ) ,
DATABASE_NAME: process . env . DB_NAME ,
PASSWORD_DATABASE: process . env . DB_PASSWORD ,
USER_DATABASE: process . env . DB_USER
}
In production:
Always use environment variables
Use strong passwords
Restrict database user privileges
Use SSL/TLS for database connections
Never expose config.js with credentials
Configuration Validation
Add validation to ensure all required config values are present:
const config = {
PORT: parseInt ( process . env . PORT ) || 3000 ,
HOST_DATABASE: process . env . DB_HOST || "localhost" ,
PORT_DATABASE: parseInt ( process . env . DB_PORT ) || 3306 ,
DATABASE_NAME: process . env . DB_NAME || "erpDB" ,
PASSWORD_DATABASE: process . env . DB_PASSWORD ,
USER_DATABASE: process . env . DB_USER || "root"
};
// Validate required fields
if ( ! config . DATABASE_NAME ) {
throw new Error ( "DATABASE_NAME is required" );
}
if ( ! config . PASSWORD_DATABASE ) {
console . warn ( "WARNING: Using default password. Set DB_PASSWORD environment variable." );
}
export default config ;
Troubleshooting
Check:
MySQL service is running: sudo systemctl status mysql
Host and port are correct
Firewall allows connection on port 3306
MySQL is listening on the correct interface:
SHOW VARIABLES LIKE 'bind_address' ;
Check:
Username and password are correct
User has permissions on the database:
SHOW GRANTS FOR 'root' @ 'localhost' ;
User exists:
SELECT User, Host FROM mysql . user ;
Solution:
Create the database:Or check the database name:
Solutions:
Change PORT in config.js to a different value (e.g., 3001)
Find and kill the process using the port:
# Linux/macOS
lsof -ti:3000 | xargs kill -9
# Windows
netstat -ano | findstr :3000
taskkill /PID < PI D > /F
Security Best Practices
Use Environment Variables
Never hardcode credentials in config.js for production environments.
Create Dedicated Database User
Don’t use the root user. Create a user with minimal required privileges: CREATE USER ' api_user '@ 'localhost' IDENTIFIED BY 'strong_password' ;
GRANT SELECT , INSERT , UPDATE , DELETE ON erpDB. * TO 'api_user' @ 'localhost' ;
Use Strong Passwords
Generate secure passwords with at least 16 characters including uppercase, lowercase, numbers, and symbols.
Restrict Database Access
Only allow connections from trusted hosts: CREATE USER ' api_user '@ '192.168.1.100' IDENTIFIED BY 'password' ;
Enable SSL/TLS
For production, configure MySQL to use SSL/TLS connections: export const pool = createPool ({
// ... other config
ssl: {
ca: fs . readFileSync ( '/path/to/ca.pem' )
}
});
Next Steps
Installation Complete installation guide
Architecture Learn how configuration is used
Database Schema Understand the database structure
Error Handling Handle connection errors