refactored server.js logic