Refactored server.js to separate out handlers and helper functions