Maintenance and Troubleshooting
This guide provides procedures for routine maintenance, common issues, and their resolutions.
Routine Maintenance
Database Maintenance
PostgreSQL:
Regular Backups: Schedule automated backups of your PostgreSQL database
Vacuum: Run
VACUUMperiodically to reclaim storage spaceAnalyze: Run
ANALYZEto update query planner statisticsConnection Pooling: Monitor connection pool usage
SQLite:
Backup Database File: Regularly copy
unilost.dbto a backup locationVacuum: Run
VACUUMto optimize database file sizeIntegrity Check: Run
PRAGMA integrity_checkto verify database integrity
Example Backup Script (PostgreSQL):
#!/bin/bash
pg_dump $DATABASE_URL > backup_$(date +%Y%m%d_%H%M%S).sql
Example Backup Script (SQLite):
#!/bin/bash
cp unilost.db backup_$(date +%Y%m%d_%H%M%S).db
Log Monitoring
Monitor server logs for:
Error Messages: Check for uncaught exceptions or database errors
Performance Issues: Monitor response times and connection counts
Security Events: Watch for failed login attempts or suspicious activity
View Logs:
# If using PM2
pm2 logs unilost
# If using systemd
journalctl -u unilost -f
# Direct output
node server.js 2>&1 | tee server.log
Session Management
Session Cleanup: Sessions expire after 6 hours automatically
Monitor Active Sessions: Check session store for active sessions
Clear Expired Sessions: Expired sessions are automatically cleaned up
Common Issues and Solutions
Port Already in Use
Error Message:
Error: listen EADDRINUSE: address already in use :::3000
Solution 1: Change Port
export PORT=3001
node server.js
Solution 2: Kill Process Using Port
# Find process using port 3000
lsof -ti:3000
# Kill the process
kill -9 $(lsof -ti:3000)
Database Connection Error
Error Message:
❌ PostgreSQL connection pool error: connect ECONNREFUSED
Solutions:
Check DATABASE_URL: Verify the connection string is correct
Check Database Server: Ensure PostgreSQL is running
Check Network: Verify network connectivity to database host
Check Credentials: Verify username and password are correct
Check Firewall: Ensure firewall allows connections on port 5432
Test Connection:
# PostgreSQL
psql $DATABASE_URL
# SQLite
sqlite3 unilost.db
Module Not Found
Error Message:
Error: Cannot find module 'express'
Solution:
npm install
Verify Installation:
npm list
Session Not Persisting
Symptoms:
User gets logged out immediately
Session data is lost on page refresh
Solutions:
Check SESSION_SECRET: Ensure it’s set and consistent
Check Cookie Settings: Verify cookies are enabled in browser
Check HTTPS: In production, ensure HTTPS is used (secure cookies)
Check SameSite: Verify SameSite cookie setting is appropriate
Debug Session:
// Add to server.js temporarily
app.get('/debug/session', (req, res) => {
res.json({
session: req.session,
cookies: req.cookies
});
});
Map Not Loading
Symptoms:
Map tiles don’t appear
Map shows gray background
Solutions:
Check Internet Connection: Map tiles are loaded from OpenStreetMap
Check Browser Console: Look for JavaScript errors
Check Leaflet.js: Verify Leaflet.js is loaded correctly
Check CORS: Ensure no CORS issues with map tile servers
Debug Map:
// Check if Leaflet is loaded
console.log(typeof L);
// Check map instance
console.log(mapRegister);
Socket.IO Connection Issues
Symptoms:
Chat messages not sending
Real-time updates not working
Solutions:
Check Socket.IO Server: Verify server is running
Check Client Connection: Verify Socket.IO client is connected
Check CORS: Ensure CORS is configured correctly
Check Firewall: Ensure WebSocket connections are allowed
Debug Socket.IO:
// Server side
io.on('connection', (socket) => {
console.log('Client connected:', socket.id);
});
// Client side
socket.on('connect', () => {
console.log('Connected to server');
});
socket.on('disconnect', () => {
console.log('Disconnected from server');
});
Database Schema Errors
Error Message:
Error: relation "users" does not exist
Solution:
The database tables are created automatically on first run. If this error occurs:
Check Database Connection: Ensure database is accessible
Check Permissions: Verify database user has CREATE TABLE permissions
Manual Initialization: Run database initialization manually
Manual Initialization:
// In db.js, call initDBPostgres() or initDBSQLite()
const { initDBPostgres, initDBSQLite } = require('./db');
if (usePostgres) {
initDBPostgres().then(() => {
console.log('Database initialized');
});
} else {
initDBSQLite();
console.log('Database initialized');
}
Performance Issues
Slow Response Times
Possible Causes:
Database queries are slow
Too many concurrent connections
Large image data in database
Missing database indexes
Solutions:
Add Database Indexes: Ensure indexes exist on frequently queried columns
Optimize Queries: Review and optimize slow queries
Connection Pooling: Adjust PostgreSQL connection pool size
Image Optimization: Compress images before storing
Caching: Implement caching for frequently accessed data
High Memory Usage
Possible Causes:
Memory leaks in code
Large session data
Too many concurrent connections
Solutions:
Monitor Memory: Use tools like
node --inspectorclinic.jsReview Code: Check for memory leaks (unclosed connections, etc.)
Limit Sessions: Reduce session max age if needed
Connection Limits: Limit database connection pool size
Security Issues
Failed Login Attempts
Monitoring:
Monitor logs for repeated failed login attempts from the same IP address.
Prevention:
Implement rate limiting for login endpoints
Use CAPTCHA for repeated failures
Implement account lockout after multiple failures
SQL Injection Prevention
Current Protection:
All queries use parameterized statements
Input validation on all endpoints
Verification:
Review all database queries to ensure they use parameterized statements:
// ✅ Good: Parameterized query
await client.query('SELECT * FROM users WHERE id = $1', [userId]);
// ❌ Bad: String concatenation (vulnerable)
await client.query(`SELECT * FROM users WHERE id = '${userId}'`);
XSS Prevention
Current Protection:
Input sanitization
Output encoding
Verification:
Ensure all user input is sanitized before storing and encoded before displaying.
Getting Help
If you encounter issues not covered in this guide:
Check Logs: Review server and browser console logs
Check Documentation: Review other documentation pages
GitHub Issues: Report issues on the GitHub repository
Community: Reach out to the project maintainers
For more information, see Configuration Guide and API Reference.