Refactored logic out of server.js and placed it into the handler along with other server logic.