refractored server.js code