refactored server.js and added tests for it