ServerSide Javascript code refactoying