Refactor code for a better server.js